-2
package com.example.submenus;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    public void onBackPressed() {
        setContentView(R.layout.activity_main);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final Button button = (Button) findViewById(R.id.a4button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.audi);
            }

        }   );

    final Button button1 = (Button) findViewById(R.id.a6button);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.audia6);
            }
        } );

        }
    }

This is the code for my main activity. In my xml files, i have 2 buttons which lead to a different view from the main menu. And when i press the back button, it comes back to the main menu of my app just as it should. Then, if i click on another button the app freezes and then crashes. I only have background in java. Does anyone know how to fix this?

Plus, here are the logs for my error from my logcat

11-18 21:16:14.674: E/AndroidRuntime(30176): FATAL EXCEPTION: main
11-18 21:16:14.674: E/AndroidRuntime(30176): java.lang.IllegalStateException: Could not find a method ButtonOnClick(View) in the activity class com.example.submenus.MainActivity for onClick handler on view class android.widget.Button with id 'a4button'
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.view.View$1.onClick(View.java:3666)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.view.View.performClick(View.java:4203)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.view.View$PerformClick.run(View.java:17189)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.os.Handler.handleCallback(Handler.java:615)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.os.Looper.loop(Looper.java:137)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.app.ActivityThread.main(ActivityThread.java:4950)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at java.lang.reflect.Method.invoke(Method.java:511)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at dalvik.system.NativeStart.main(Native Method)
11-18 21:16:14.674: E/AndroidRuntime(30176): Caused by: java.lang.NoSuchMethodException: ButtonOnClick [class android.view.View]
11-18 21:16:14.674: E/AndroidRuntime(30176):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at java.lang.Class.getMethod(Class.java:915)
11-18 21:16:14.674: E/AndroidRuntime(30176):    at android.view.View$1.onClick(View.java:3659)
11-18 21:16:14.674: E/AndroidRuntime(30176):    ... 11 more
Steve Sahayadarlin
  • 1,164
  • 3
  • 15
  • 32
  • If it crashes, please give us the stack trace (log cat) of the exception making the crash. –  Nov 19 '13 at 00:20
  • First of all you may want try to include `super.onBackPressed();` before `setContentView(R.layout.activity_main);` inside `onBackPressed` – Shobhit Puri Nov 19 '13 at 00:26
  • 1
    You should use `Fragment` rather than switching different layouts from Activity – Glenn Nov 19 '13 at 00:31

2 Answers2

1

If you are setting two different views on a button click, why don't you create two different activities and set content those views in those activities.

Then you can call those activities from those button click listeners. It is fast efficient and your app will not crash.

java.lang.IllegalStateException: Could not find a method ButtonOnClick(View) in the activity class com.example.submenus.MainActivity for onClick handler on view class android.widget.Button with id 'a4button'

This errors means that you do not have a method named ButtonOnClick(View) for button with id a4button.

Remove those final modifiers also

Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
0

Your problems are from calling setContentView() multiple times. Use a ViewFlipper with setDisplayedChild() or a FrameLayout with multiple views, or start a new Activity for each button click. See Calling setContentView() multiple times

Community
  • 1
  • 1
Steve M
  • 9,296
  • 11
  • 49
  • 98