I have created two activities and added an onClick button to go to the second activity from the first which works just fine …
But when i try to add a “back” button to the second activity to go back to the first activity
when i run it in the emulator when i click on back it says that the app has stopped working and the logcat is as follows
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)
Heres a snippet from my code
the 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);
}
the clubsactivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void back(View v) {
setContentView(R.layout.activity_main);
}
this is not the correct way to start a new activity.
you only change the views on the screen.
to start a new activity:
on clubs function (button click action) add this code
Intent intent = new Intent(CurrentActivity.this, SecondActivity.class); startActivity(intent);
and you dont need a back button on the second activity.
android has a back button this not an iOS.
you must read more about android development.