不幸的是,该应用已停止Android Studio

时间:2017-06-16 02:21:03

标签: java android google-apps-script

我很确定我的XML或清单没有问题。问题必须出在我的代码中(因为我稍微修改了我的代码并且它停止了工作)。该应用程序甚至在开始之前就崩溃了。我得到的错误是:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.trainer.braintrainer / com.trainer.braintrainer.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.widget.Button.setText(java.lang.CharSequence)'为null   对象参考                                                                                 在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                                                 在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                 在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                 在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                 在android.os.Looper.loop(Looper.java:148)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                                                                              引起:java.lang.NullPointerException:尝试调用虚拟   方法'void android.widget.Button.setText(java.lang.CharSequence)'on   空对象引用                                                                                 在   com.trainer.braintrainer.MainActivity.generateQuestion(MainActivity.java:60)                                                                                 在   com.trainer.braintrainer.MainActivity.onCreate(MainActivity.java:101)                                                                                 在android.app.Activity.performCreate(Activity.java:6237)                                                                                 在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                                 在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                                                                                 在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                 在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                 在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                 在android.os.Looper.loop(Looper.java:148)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)06-16   08:01:16.547 24091-24091 / com.trainer.braintrainer I / Process:发送   信号。 PID:24091 SIG:9

以下是我的主要java代码:

package com.trainer.braintrainer;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    ArrayList<Integer> answers=new ArrayList<Integer>();
    Random rand = new Random();

    int locationOfCorrectAnswer;
    Button startButton;
    Button button0;
    Button button1;
    Button button2;
    Button button3;
    int score=0;
    int numberOfQuestions=0;
    TextView answerMessage;
    String message;
    TextView scoreText;
    TextView sumTextView;
    TextView timerText;

    public void generateQuestion(){

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

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

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

                }
                answers.add(inCorrectAns);
            }
        }
        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)));
    }

    public void startButton(View view){
        startButton.setVisibility(View.INVISIBLE);
    }
    public void answerFunction(View view){
         int tappedLocation= (int)view.getTag();

        if (tappedLocation==locationOfCorrectAnswer){
            message="Correct!";
            score++;
        }else{
            message="Wrong!";
        }
        answerMessage.setText(message);
        scoreText.setText(Integer.toString(score) + "/" + Integer.toString(numberOfQuestions));
        numberOfQuestions++;
        generateQuestion();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton=(Button)findViewById(R.id.button5);
        answerMessage=(TextView)findViewById(R.id.messageView);
        sumTextView=(TextView)findViewById(R.id.sumTextView);
        scoreText=(TextView)findViewById(R.id.scoreText);
        timerText=(TextView)findViewById(R.id.timerText);

        generateQuestion();
        new CountDownTimer(30100,1000){

            @Override
            public void onTick(long millisUntilFinished) {
                int seconds=(int) millisUntilFinished/1000;
                timerText.setText(Integer.toString(seconds)+"s");
            }

            @Override
            public void onFinish() {
                answerMessage.setText("Done");
            }
        }.start();
        button0=(Button)findViewById(R.id.ans1);
        button1=(Button)findViewById(R.id.ans2);
        button2=(Button)findViewById(R.id.ans3);
        button3=(Button)findViewById(R.id.ans4);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在调用方法generateQuestion();在为按钮赋值之前:button1,button2等。当你在这些按钮上执行setText时,它会抛出一个空对象引用错误。你能做的是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startButton=(Button)findViewById(R.id.button5);
    answerMessage=(TextView)findViewById(R.id.messageView);
    sumTextView=(TextView)findViewById(R.id.sumTextView);
    scoreText=(TextView)findViewById(R.id.scoreText);
    timerText=(TextView)findViewById(R.id.timerText);

    button0=(Button)findViewById(R.id.ans1);
    button1=(Button)findViewById(R.id.ans2);
    button2=(Button)findViewById(R.id.ans3);
    button3=(Button)findViewById(R.id.ans4);

    generateQuestion();
    new CountDownTimer(30100,1000){

        @Override
        public void onTick(long millisUntilFinished) {
            int seconds=(int) millisUntilFinished/1000;
            timerText.setText(Integer.toString(seconds)+"s");
        }

        @Override
        public void onFinish() {
            answerMessage.setText("Done");
        }
    }.start();

}
}

这是在调用generateQuestion()

之前为按钮分配相应的视图