为什么在激活onClick功能时出现错误



我创建了Game Over窗口,其中有Restart和Exit按钮。

<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函数(exit((和restart(((在GameOver类中进行了描述

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

问题退出功能没有响应,重新启动功能产生此错误:

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) 

我从这个错误中了解到,重新启动方法出了问题。与这行完全一致:

startActivity(intent);

从异常来看,您似乎没有将Game活动添加到清单中。

由以下原因引起:android.content.ActivityNotFoundException:找不到显式活动类{com.example.breakeoutgame/com.example.bleakeoutgame.Game};您是否在AndroidManifest.xml中声明了此活动?

只需将Game活动添加到AndroidManifest.xml中,它就可以工作了。

相关内容

  • 没有找到相关文章

最新更新