更新时间:2023-11-16 19:38:34
在你的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.