我是安卓新手。我已经开始使用 ActivityLifeCycle 应用程序。在这我有3个活动课程。从第一个活动开始,我想在第一个活动中单击按钮时使用意图进入第二个活动类。但它给出了错误。我已经导入了正确的android.view.View
包。
之前有人问过同样的问题,但我没有得到解决方案,这是容易出错的代码。
活动主.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:(这是第一个活动)
受保护的无效startActivityB(查看v){
Intent intent = new Intent(getApplicationContext(), Activity_B.class);
startActivity(intent);
}
protected void finishA(View v)
{
MainActivity.this.finish();
}
这是我在 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)