有谁知道如何解决这个错误,谢谢!!我认为 MainActivity 中的“public void 选择答案”有问题,因为然后我尝试按 4 个按钮中的 1 个按钮,出现此错误并且应用程序崩溃。错误信息:FATAL EXCEPTION: main Process: com.example.braintrainer, PID: 19387 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154)
3 回答
炎炎设计
TA贡献1808条经验 获得超4个赞
尝试将标签设置为您的,Buttons
如下所示:
<Button android:id="@+id/button0" android:tag="1" // this line android:layout_width="0dp" android:layout_height="0dp" android:background="#9C27B0" android:onClick="chooseAnswer" android:text="3" android:textSize="36sp" app:layout_constraintBottom_toTopOf="@+id/guideline2" app:layout_constraintEnd_toStartOf="@+id/guideline" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
哔哔one
TA贡献1854条经验 获得超8个赞
原来是NullPointerException
这个原因造成的 请检查
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){ Log.i("TAG", String.valueOf(view.getTag())); }
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
作为view.getTag()
返回 null。
添加回答
举报
0/150
提交
取消