我创建了两个活动并添加了一个 onClick 按钮以从第一个活动转到第二个活动,效果很好......但是当我尝试向第二个活动添加一个“返回”按钮以返回第一个活动时当我点击返回时在模拟器中运行它说应用程序已停止工作并且logcat如下
10-07 01:28:13.385: E/AndroidRuntime(628): FATAL EXCEPTION: main
10-07 01:28:13.385: E/AndroidRuntime(628): java.lang.IllegalStateException: Could not find a method back(View) in the activity class com.example.app2.MainActivity for onClick handler on view class android.widget.Button
10-07 01:28:13.385: E/AndroidRuntime(628): at android.view.View$1.onClick(View.java:3578)
10-07 01:28:13.385: E/AndroidRuntime(628): at android.view.View.performClick(View.java:4084)
10-07 01:28:13.385: E/AndroidRuntime(628): at android.view.View$PerformClick.run(View.java:16966)
10-07 01:28:13.385: E/AndroidRuntime(628): at android.os.Handler.handleCallback(Handler.java:615)
10-07 01:28:13.385: E/AndroidRuntime(628): at android.os.Handler.dispatchMessage(Handler.java:92)
10-07 01:28:13.385: E/AndroidRuntime(628): at android.os.Looper.loop(Looper.java:137)
10-07 01:28:13.385: E/AndroidRuntime(628): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-07 01:28:13.385: E/AndroidRuntime(628): at java.lang.reflect.Method.invokeNative(Native Method)
这是我的代码中的一个片段 mainactivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clubs(View v1) {
setContentView(R.layout.activity_display_clubs);
}
clubsactivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void back(View v) {
setContentView(R.layout.activity_main);
}