简体   繁体   English

java.lang.IllegalStateException:无法为 android 执行方法:onClick / ZE84E30B9390CDB67ZDDB6

[英]java.lang.IllegalStateException: Could not execute method for android:onClick / Android

Does any one know how to fix this error, THANK YOU,.有谁知道如何解决这个错误,谢谢。 I think something wrong with "public void choose answer" in MainActivity, because then i try press on 1 of 4 buttons I get this error and app crash.我认为 MainActivity 中的“公共无效选择答案”有问题,因为然后我尝试按下 4 个按钮中的 1 个按钮我收到此错误并且应用程序崩溃。

Error message:错误信息:

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) 
        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.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.example.braintrainer.MainActivity.chooseAnswer(MainActivity.java:24)
        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) 
        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) 

Here all buttons have same onClick (maybe here something wrong?)- android:onClick="chooseAnswer" activity_main.xml这里所有按钮都有相同的 onClick (可能这里有问题?)- android:onClick="chooseAnswer" activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#4CAF50"
        android:onClick="start"
        android:text="Go!"
        android:textSize="60sp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#4CAF50"
            android:text="30s"
            android:textAlignment="center"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/sumTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="31 + 12"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="25sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#03A9F4"
            android:text="0/0"
            android:textAlignment="center"
            android:textSize="25sp" />
    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/gridLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constrainedHeight="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toTopOf="@+id/resultTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintWidth_max="300dp">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5" />

        <Button
            android:id="@+id/button0"
            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" />

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#CDDC39"
            android:onClick="chooseAnswer"
            android:text="3"
            android:textSize="36sp"
            app:layout_constraintBottom_toTopOf="@+id/guideline2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/guideline"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF5722"
            android:onClick="chooseAnswer"
            android:text="3"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/guideline"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#4CAF50"
            android:onClick="chooseAnswer"
            android:text="3"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/guideline"
            app:layout_constraintTop_toBottomOf="@+id/guideline2" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Correct!"
        android:textSize="40sp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/startButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here Im trying call buttons from "public void chooseAnswer(View view)" to check if they are working and Im getting their id MainActivity.java在这里,我尝试从“public void chooseAnswer(View view)”调用按钮来检查它们是否正在工作,并且我正在获取它们的 ID MainActivity.java

package com.example.braintrainer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    Button startButton;
    TextView resultTextView;
    ArrayList<Integer> answers = new ArrayList<Integer>();
    int locationOfCorrectAnswer;
    int score = 0;

    public void chooseAnswer(View view){

        if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
            Log.i("TAG", String.valueOf(view.getTag()));
        }
    }

    public void start(View view){

        startButton.setVisibility(View.INVISIBLE);

    }

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

        startButton=(Button)findViewById(R.id.startButton);
        TextView sumTextView = (TextView)findViewById(R.id.sumTextView);
        Button button0 = (Button)findViewById(R.id.button0);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        resultTextView = (TextView)findViewById(R.id.resultTextView);


        Random rand = new Random();

        int a = rand.nextInt(21);
        int b = rand.nextInt(21);

        sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));

        locationOfCorrectAnswer = rand.nextInt(4);
        int incorrectAnswer;

        for (int i=0; i<4; i++){
            if (i == locationOfCorrectAnswer){
                answers.add(a + b);
            } else {
                incorrectAnswer = rand.nextInt(41);
                while (incorrectAnswer == a + b){
                    incorrectAnswer = rand.nextInt(41);
                }
                answers.add(incorrectAnswer);
            }
        }

        button0.setText(Integer.toString(answers.get(0)));
        button1.setText(Integer.toString(answers.get(1)));
        button2.setText(Integer.toString(answers.get(2)));
        button3.setText(Integer.toString(answers.get(3)));

    }
}

AndroidManifest.xml AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.braintrainer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

You try to check buttons by tag but you didn't set any tags in xml您尝试按标签检查按钮,但您没有在 xml 中设置任何标签

It's a NullPointerException caused by this.这是由此引起的NullPointerException Please check请检查

    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

as view.getTag() return null.作为view.getTag()返回 null。

try to set the tag to your Buttons like this:尝试将标签设置为您的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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick in android 错误:java.lang.IllegalStateException:无法执行android:onClick的方法 - Error: java.lang.IllegalStateException: Could not execute method for android:onClick Kotlin-java.lang.IllegalStateException:无法执行android:onClick的方法 - Kotlin - java.lang.IllegalStateException: Could not execute method for android:onClick Android-Java:java.lang.IllegalStateException:无法执行 android 的方法:onClick - Android-Java: java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio 问题; java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Android Studio problem ; java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio Application - java.lang.IllegalStateException: Could not execute method for android:onClick - Not a Null Pointer Exception - Android Studio Application - java.lang.IllegalStateException: Could not execute method for android:onClick - Not a Null Pointer Exception Android Studio 3.1.3 中的错误。 java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Error in Android Studio 3.1.3. java.lang.IllegalStateException: Could not execute method for android:onClick ANDROID意图致命错误!!! java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - ANDROID INTENTION FATAL ERROR!!! java.lang.IllegalStateException: Could not execute method for android:onClick java.lang.IllegalStateException:尝试单击按钮时无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick when trying to click a button Tictac toe 游戏不断停止并出现 logcat 错误 java.lang.IllegalStateException:Could not execute method for android:onClick - Tictac toe game keeps stopping with logcat error java.lang.IllegalStateException:Could not execute method for android:onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM