有誰(shuí)知道如何解決這個(gè)錯(cuò)誤,謝謝??!我認(rèn)為 MainActivity 中的“public void 選擇答案”有問(wèn)題,因?yàn)槿缓笪覈L試按 4 個(gè)按鈕中的 1 個(gè)按鈕,出現(xiàn)此錯(cuò)誤并且應(yīng)用程序崩潰。錯(cuò)誤信息: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 回答

炎炎設(shè)計(jì)
TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試將標(biāo)簽設(shè)置為您的,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貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
原來(lái)是NullPointerException
這個(gè)原因造成的 請(qǐng)檢查
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。
添加回答
舉報(bào)
0/150
提交
取消