Java 在android studio中尝试打印随机值时出现NullPointerException

Java 在android studio中尝试打印随机值时出现NullPointerException,java,android,random,nullpointerexception,settext,Java,Android,Random,Nullpointerexception,Settext,我一直在第99行和第77行得到一个空指针异常,在这里我试图打印由random类创建的随机值(第99行),并在这里调用我的setQuestion方法(第77行。layout_game xml文件的randomNum。我不确定为什么会得到空值,因为当我将值记录到控制台时,它表示正在生成值 package com.grantsolutions.mathgamechapter1; import android.app.Activity; import android.content.Intent; i

我一直在第99行和第77行得到一个空指针异常,在这里我试图打印由random类创建的随机值(第99行),并在这里调用我的setQuestion方法(第77行。layout_game xml文件的randomNum。我不确定为什么会得到空值,因为当我将值记录到控制台时,它表示正在生成值

package com.grantsolutions.mathgamechapter1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;


public class GameActivity extends Activity implements OnClickListener {
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
TextView textObjectPartA //it says that textObjectPartA and B are never used;
TextView textObjectPartB;
TextView textObjectScore;
TextView textObjectLevel;
int currentScore;
int currentLevel = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_game);






    //declaring variables for game

    //no longer being declared

    /*int partA = 2;
    int partB = 2;
    correctAnswer = partA * partB;
    int wrongAnswer1 = correctAnswer - 1;
    int wrongAnswer2 = correctAnswer + 1;
    /*this is where we link the variables
    from  the java code to the button UI in
     activity_game xml itself
     */
    TextView textObjectPartA = (TextView) findViewById(R.id.holdTextA);

    TextView textObjectPartB = (TextView) findViewById(R.id.holdTextB);
    textObjectScore = (TextView) findViewById(R.id.textLevel);
    textObjectLevel = (TextView) findViewById(R.id.textScore);

    buttonObjectChoice1 =
            (Button) findViewById(R.id.choice1);

    buttonObjectChoice2 =
            (Button) findViewById(R.id.choice2);

    buttonObjectChoice3 =
            (Button) findViewById(R.id.choice3);
    /*Use the setText method of the class on our objects
    to show our variable values on the UI elements.
    Similar to outputting to the console in the exercise,
    only using the setText method*/


    //telling the console to listen for button clicks from the user
    buttonObjectChoice1.setOnClickListener(this);
    buttonObjectChoice2.setOnClickListener(this);
    buttonObjectChoice3.setOnClickListener(this);
    setQuestion();



}


void setQuestion() {
    Random randomNum;
    randomNum = new Random();

    int partA = 0;
    int numberRange = currentLevel * 3;
    partA = randomNum.nextInt(numberRange) + 1;
     //removes possibility of a zero value
    int partB = randomNum.nextInt(numberRange) + 1;

    correctAnswer = partA * partB;
    Log.i("info", ""+partA);
    Log.i("info", ""+partB);
    Log.d("info", ""+partA);
    int wrongAnswer1 = correctAnswer - 2;
    int wrongAnswer2 = correctAnswer + 2;
    Log.i("info", ""+partA);
    Log.i("info", ""+partB);
    textObjectPartA.setText(""+partA); //error here
    textObjectPartB.setText(""+partB);
    //case for setting multiple choice buttons
    int buttonLayout = randomNum.nextInt(3);
    switch (buttonLayout) {
        case 0:
            buttonObjectChoice1.setText("" + correctAnswer);
            buttonObjectChoice2.setText("" + wrongAnswer1);
            buttonObjectChoice3.setText("" + wrongAnswer2);
            break;
        case 1:
            buttonObjectChoice2.setText("" + correctAnswer);
            buttonObjectChoice1.setText("" + wrongAnswer1);
            buttonObjectChoice3.setText("" + wrongAnswer2);
            break;
        case 2:
            buttonObjectChoice3.setText("" + correctAnswer);
            buttonObjectChoice1.setText("" + wrongAnswer1);
            buttonObjectChoice2.setText("" + wrongAnswer2);
            break;
    }


}

void updateScoreAndLevel(int answerGiven) {
    if (isCorrect(answerGiven)) {
        for (int i = 1; i <= currentLevel; i++) {
            currentScore = currentScore + i;

        }
    } else {
        currentLevel = 1;
        currentScore = 0;

    }

    currentLevel++;

}


boolean isCorrect(int answerGiven) {
    boolean correctTrueOrFalse = true;
    if (answerGiven == correctAnswer) {//YAY!
        Toast.makeText(getApplicationContext(), "Well Done", Toast.LENGTH_LONG).show();

    }
    else {//If wrong print this instead
        Toast.makeText(getApplicationContext(), "You're wrong", Toast.LENGTH_SHORT).show();
    }  //case for each button being made
    correctTrueOrFalse = false;

    return correctTrueOrFalse;
}


    @Override
    public void onClick (View view){
        //declaring a new int to use in all cases
        int answerGiven;

        switch (view.getId()) {
            //case for each button being made

            case R.id.choice1:
                //button1 information goes here
                //initialize a new integer with the value
                //contained in buttonObjectChoice1
                answerGiven = Integer.parseInt("" +       buttonObjectChoice1.getText());
                //check if the right answer

                break;

            case R.id.choice2:
                //button2 information goes here
                answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());


                break;

            case R.id.choice3:
                //button 3 information goes here
                answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
                break;


        }

    }


}
第99行

   Log.i("info", ""+partA);
    Log.i("info", ""+partB); 
    textObjectPartA.setText(""+partA); //error here
    textObjectPartB.setText(""+partB);
游戏文件的布局

<?xml version="1.0" encoding="utf-8"?>


在您的

onCreateView()
你已经申报了

TextView textObjectPartA = (TextView) findViewById(R.id.holdTextA);

这样,您的全局变量textObjectPartA就没有设置,因此出现了空点异常。

由于代码太多,我想以常见问题(大家都知道它是什么)的副本和离题的形式结束。唉,我必须选择一个…:(你也可以粘贴异常吗?我应该把它放在哪里,在oncreate部分之外?它只应该是'textObjectPartA=(TextView)findViewById(R.id.holdTextA);'这样你的全局变量textObjectPartA将有一个不为null的值。这就是为什么它说从来没有使用过textObjectPartA的原因(阅读您的评论)“TextView textObjectPartA=(TextView)findViewById(R.id.holdTextA);”正在为onCreate方法创建一个局部变量。非常感谢,我甚至没有看到这个问题。#每个人都不会发生这种情况
onCreateView()
TextView textObjectPartA = (TextView) findViewById(R.id.holdTextA);