【问题标题】:Null pointer Exception in onCreate Method: Attempt to invoke virtual method on a null objectonCreate 方法中的空指针异常:尝试在空对象上调用虚拟方法
【发布时间】:2020-04-08 11:20:41
【问题描述】:

是我遗漏了什么还是我的代码做错了什么?它在我运行时显示:

原因:java.lang.NullPointerException:尝试调用虚拟 方法 'void android.widget.TextView.setText(java.lang.CharSequence)' 在空对象引用上 com.haarismemon.forensiclab.test2.onCreate(test2.java:47)

activity_test2.xml

<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=".test2">


    <ProgressBar
        android:id="@+id/prog_timer"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:max="30"
        android:progress="45"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/prog_timer" />

    <TextView
        android:id="@+id/tv_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/prog_timer" />

    <TextView
        android:id="@+id/question"
        android:layout_width="415dp"
        android:layout_height="150dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="30dp"
        android:text="TextView"
        android:textSize="24sp"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="@id/tv_timer"
        app:layout_constraintStart_toStartOf="@id/tv_score"
        app:layout_constraintTop_toBottomOf="@id/prog_timer" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/question">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/btn_answer0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="200dp"
                android:background="#FFC300 "
                android:text="Button" />

            <Button
                android:id="@+id/btn_answer1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="200dp"
                android:background="#FFFC33"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/btn_answer2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="200dp"
                android:background="#FF5733 "
                android:text="Button" />

            <Button
                android:id="@+id/btn_answer3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="200dp"
                android:background="#C70039"
                android:text="Button" />
        </TableRow>
    </TableLayout>

    <TextView
        android:id="@+id/tv_bottommessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

test2.java


import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class test2 extends AppCompatActivity {
    Button  btn_answer0, btn_answer1, btn_answer2, btn_answer3;
    TextView question,  tv_timer, tv_bottommessage, tv_score;
    ProgressBar prog_timer;

    private Question2 mQuestion = new Question2 ();
    private String mAnswer;
    private int mScore = 0;
    private int mQuestionLength = mQuestion.myQuestion.length;


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

        final Random r = new Random();


        btn_answer0 = findViewById(R.id.btn_answer0);
        btn_answer1 = findViewById(R.id.btn_answer1);
        btn_answer2 = findViewById(R.id.btn_answer2);
        btn_answer3 = findViewById(R.id.btn_answer3);
        tv_score = findViewById(R.id.tv_score);
        question = findViewById(R.id.question);
        tv_bottommessage = findViewById(R.id.tv_bottommessage);
        tv_timer = findViewById(R.id.tv_timer);
        prog_timer = findViewById(R.id.prog_timer);

        tv_score.setText("Score: "+ mScore);

        updatedCurrentQuestion(r.nextInt(mQuestionLength));

        btn_answer0.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (btn_answer0.getText()==mAnswer) {
                    mScore++;
                    tv_score.setText("Score: " + mScore);
                    updatedCurrentQuestion(r.nextInt(mQuestionLength));
                }else{
                    gameisover();
                }
            }
        });

        btn_answer1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (btn_answer1.getText()==mAnswer) {
                    mScore++;
                    tv_score.setText("Score: " + mScore);
                    updatedCurrentQuestion(r.nextInt(mQuestionLength));
                }else{
                    gameisover();
                }
            }
        });

        btn_answer2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (btn_answer2.getText()==mAnswer) {
                    mScore++;
                    tv_score.setText("Score: " + mScore);
                    updatedCurrentQuestion(r.nextInt(mQuestionLength));
                }else{
                    gameisover();
                }
            }
        });

        btn_answer3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (btn_answer3.getText()==mAnswer) {
                    mScore++;
                    tv_score.setText("Score: " + mScore);
                    updatedCurrentQuestion(r.nextInt(mQuestionLength));
                }else{
                    gameisover();
                }
            }
        });
    }

    private void gameisover(){
        AlertDialog.Builder alertDialogbuilder = new AlertDialog.Builder(test2.this);
        alertDialogbuilder
                .setMessage("YOUR GAME IS OVER!"+ mScore)
                .setCancelable(false)
                .setPositiveButton("Start New Game.", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivity(new Intent(getApplicationContext(), test2.class));
                        finish();
                    }
                })
                .setNegativeButton("Exit.", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
        AlertDialog alertDialog = alertDialogbuilder.create();
        alertDialog.show();
    }

    private void updatedCurrentQuestion(int i){
        question.setText(mQuestion.getQuestion(i));
        btn_answer0.setText(mQuestion.getChoices0(i));
        btn_answer1.setText(mQuestion.getChoices1(i));
        btn_answer2.setText(mQuestion.getChoices2(i));
        btn_answer3.setText(mQuestion.getChoices3(i));
        mAnswer = mQuestion.getCorrectAnswer(i);

    }
}

【问题讨论】:

  • 请更改标题以包含您的问题的简短描述。
  • 似乎您在 findViewById() 中使用了错误的 TextView id
  • 引用错误信息。

标签: javascript android android-studio android-layout


【解决方案1】:

这个错误通常发生在使用findViewById方法错误匹配widget的id时。尝试从这里更改您的代码:

tv_score = (TextView)findViewById(R.id.tv_score);

到这里:

tv_score = findViewById(R.id.tv_score);

另外,重新检查 findViewByIdmethod 中使用的 id。

【讨论】:

    【解决方案2】:

    现在我知道问题是什么了..问题是

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mobile_forensic);
    

    应该是这样的

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test2);
    

    对不起我的英语不好..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多