我是新来的Android。我已经开始与ActivityLifeCycle应用程序在做。在此我有3个活动课。从第一个活动我想用意图被点击1日的活动按钮时要到第二个活动类。但它给错误。我已导入正确的 android.view.View
包。
同样的问题被要求别人更早,但我没有得到解决, 这里是容易出错的code。
activity_main.xml:
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / textView1
机器人:layout_marginTop =22dp
机器人:layout_toRightOf =@ + ID / textView1
机器人:的onClick =startActivityB
机器人:可点击=真
机器人:文本=startb/>
<按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID /按钮1
机器人:layout_below =@ + ID /按钮1
机器人:layout_marginTop =31dp
机器人:的onClick =finishA
机器人:可点击=真
机器人:文本=FinishA/>
<按钮
MainActivity.java:(这是第一活性)
保护无效startActivityB(视图v){
意向意图=新的意图(getApplicationContext(),Activity_B.class);
startActivity(意向);
}
保护无效finishA(视图v)
{
MainActivity.this.finish();
}
这是我在logcat中得到了错误:
D / Avtivity_A(1333):ONSTART()的Activity_A开始
D / Avtivity_A(1333):onResume()Activity_A开始
D / AndroidRuntime(1333):关闭虚拟机
W / dalvikvm(1333):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
E / AndroidRuntime(1333):致命异常:主要
E / AndroidRuntime(1333):java.lang.IllegalStateException:找不到方法
finishA在活动课com.example.lifecycle.MainActivity为(视图)
在视图类android.widget.Button onclick处理程序ID为按钮2
E / AndroidRuntime(1333):在android.view.View $ 1.onClick(View.java:2059)
E / AndroidRuntime(1333):在android.view.View.performClick(View.java:2408)
E / AndroidRuntime(1333):在android.view.View $ PerformClick.run(View.java:8816)
E / AndroidRuntime(1333):在android.os.Handler.handleCallback(Handler.java:587)
E / AndroidRuntime(1333):在android.os.Handler.dispatchMessage(Handler.java:92)
E / AndroidRuntime(1333):在android.os.Looper.loop(Looper.java:123)
E / AndroidRuntime(1333):在android.app.ActivityThread.main(ActivityThread.java:4627)
E / AndroidRuntime(1333):在java.lang.reflect.Method.invokeNative(本机方法)
E / AndroidRuntime(1333):在java.lang.reflect.Method.invoke(Method.java:521)
E / AndroidRuntime(1333):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
E / AndroidRuntime(1333):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E / AndroidRuntime(1333):在dalvik.system.NativeStart.main(本机方法)
解决方案
在你的XML为你定义一个onClick处理程序中的活动,而且这名处理程序的函数名。我想你没有实现的功能。
即
在XML有:
安卓的onClick =finishA
和在类必须实现的功能:
公共无效finishA(查看视图)
{
}
不知道这是原因,但它看起来像它。如果不是,则张贴XML和类(它的相关部分)。
更新
在你的问题中提到的错误BUTTON2和你的XML是关于Button1的,所以你应该向我们展示了正确的文件。
更新
您onclick处理程序是保护
,但它需要公开
作为样本中我发现上面。当我测试在我的应用程序,我得到同样的错误,当我把它保护。
I am new to Android. I have started doing with ActivityLifeCycle app. In this I have 3 activity classes. From first activity I want to go to second activity class using intents when a button is clicked in the 1st activity. But it is giving error. And I have imported correct android.view.View
package.
The same question was asked by someone else earlier, but I didn't get the solution, here is the error-prone code.
activity_main.xml:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="22dp"
android:layout_toRightOf="@+id/textView1"
android:onClick="startActivityB"
android:clickable="true"
android:text="startb" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="31dp"
android:onClick="finishA"
android:clickable="true"
android:text="FinishA" />
<Button
MainActivity.java: (and this is the 1st activity)
protected void startActivityB(View v) {
Intent intent = new Intent(getApplicationContext(), Activity_B.class);
startActivity(intent);
}
protected void finishA(View v)
{
MainActivity.this.finish();
}
This are the errors I got in logcat:
D/Avtivity_A(1333): onStart()of Activity_A started
D/Avtivity_A(1333): onResume()Activity_A started
D/AndroidRuntime(1333): Shutting down VM
W/dalvikvm(1333): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(1333): FATAL EXCEPTION: main
E/AndroidRuntime(1333): java.lang.IllegalStateException: Could not find a method
finishA(View) in the activity class com.example.lifecycle.MainActivity for
onClick handler on view class android.widget.Button with id 'button2'
E/AndroidRuntime(1333): at android.view.View$1.onClick(View.java:2059)
E/AndroidRuntime(1333): at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(1333): at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(1333): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(1333): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1333): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(1333): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(1333): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1333): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(1333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(1333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(1333): at dalvik.system.NativeStart.main(Native Method)
解决方案
In your xml for the activity you defined an onClick handler, and this name is the function name of the handler. I think you didn't implement the function.
i.E.
in the XML you have :
android:onClick="finishA"
and in the class you must implement a function:
public void finishA(View view)
{
}
Not sure if this is the cause, but it looks like it. If not, then post the XML and the class (the relevant parts of it).
Update
In your question the error references button2 and your XML is about button1, so you should show us the correct files.
Update
Your onClick handler is protected
but it needs to be public
as in the sample I showed above. When I test this in my app I get the same error when I make it protected.
相关推荐
最新文章