javaandroidandroid-studioonclickbreakout

Why I get an error when onClick function is activated?


I created Game Over window which has Restart and Exit buttons.

       <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/exit"
            android:onClick="exit" />
      <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/restart"
            android:onClick="restart" />

onClick functions (exit() and restart()) are described in GameOver class

    public void restart(View view) {
        Intent intent = new Intent(GameOver.this, Game.class);
        startActivity(intent);
        finish();
    }

    public void exit(View view) {
        finish();
    }

Problem Exit function isn't responsive and Restart function yield this error:

2022-09-15 22:37:25.541 12951-12951/com.example.breakoutgame E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.breakoutgame, PID: 12951
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
        at android.view.View.performClick(View.java:7448) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.breakoutgame/com.example.breakoutgame.Game}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
        at android.app.Activity.startActivityForResult(Activity.java:5320)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:712)
        at android.app.Activity.startActivityForResult(Activity.java:5278)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:693)
        at android.app.Activity.startActivity(Activity.java:5664)
        at android.app.Activity.startActivity(Activity.java:5617)
        at com.example.breakoutgame.GameOver.restart(GameOver.java:23)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
        at android.view.View.performClick(View.java:7448) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

What I understand from this error is that something went wrong with restart method. exactly with this line:

 startActivity(intent);

Solution

  • From the exception, it seems you did not add Game activity to your manifest.

    Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.breakoutgame/com.example.breakoutgame.Game}; have you declared this activity in your AndroidManifest.xml?

    Just add Game activity to your AndroidManifest.xml and it should work.