java - Android 应用程序在 TextView 中启动 Intent 并使用 setText() 后崩溃

标签 java android android-intent crash

我尝试使用 Intent 启动一个 Activity ,并在启动的 Activity 中的 TextView 中设置值,但是当我尝试这样做时,我的应用程序崩溃了。这是我试图启动的 Activity 。当我尝试在最后两行代码中设置文本时,它崩溃了

public class GameOver extends Activity {

  TextView correctTxt;
  TextView incorrectTxt;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Intent intent = getIntent();
      int correct = intent.getIntExtra("correct", 0);
      int incorrect = intent.getIntExtra("incorrect", 0);

      correctTxt = (TextView)findViewById(R.id.correct_txt);
      incorrectTxt = (TextView)findViewById(R.id.incorrect_txt);

      correctTxt.setText("" + correct);
      incorrectTxt.setText("" + incorrect);
  }
}

以及正在启动的 Activity 。我利用 trueButton onClickListener 方法中的 Intent :

package com.example.beithia.geotest;

import android.app.Activity;
import java.util.*;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;


public class TestActivity extends Activity {

  private Button trueButton;
  private Button falseButton;
  private TextView questionTextView;
  private TextView correctNum;
  private TextView incorrectNum;
  private TextView avgScore;
  int numOfCorrect = 0;
  int numOfIncorrect = 0;
  double num = 1;

  private TrueFalse[] questionBank = new TrueFalse[] {
    new TrueFalse(R.string.question_oceans, true),
    new TrueFalse(R.string.question_mideast, false),
    new TrueFalse(R.string.question_americas, true),
    new TrueFalse(R.string.question_asia,true),
    new TrueFalse(R.string.question_channel_islands,true),
    new TrueFalse(R.string.question_china,false),
    new TrueFalse(R.string.question_mexico,true),
    new TrueFalse(R.string.question_turkey,false),
    new TrueFalse(R.string.question_everest,false),
    new TrueFalse(R.string.question_colombia,false),
    new TrueFalse(R.string.question_vatican,true),
    new TrueFalse(R.string.question_nile,true),
  };

  private int currentIndex = 0;

  private void updateQuestion() {
    int question = questionBank[currentIndex].getQuestion();
    questionTextView.setText(question);
  }

  private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = questionBank[currentIndex].isTrueQuestion();
    int msgId = 0;
    if (userPressedTrue == answerIsTrue) {
        msgId = R.string.correct_toast;
        numOfCorrect++;
    }
    else {
        msgId = R.string.incorrect_toast;
        numOfIncorrect++;
    }
    Toast.makeText(this,msgId,Toast.LENGTH_SHORT).show();
  }


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

    questionTextView = (TextView)findViewById(R.id.question_text_view);
    correctNum = (TextView)findViewById(R.id.correct_num);
    incorrectNum = (TextView)findViewById(R.id.incorrect_num);
    avgScore = (TextView)findViewById(R.id.average_score);


    trueButton = (Button) findViewById(R.id.true_button);
    trueButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          checkAnswer(true);
          double score = (numOfCorrect / num) * 100;
          currentIndex = (currentIndex + 1) % questionBank.length;
          correctNum.setText("Correct: " + numOfCorrect);
          incorrectNum.setText("Incorrect: " + numOfIncorrect);
          avgScore.setText("Score: " + String.format("%.2f", score) + "%");
          updateQuestion();
          num++;
          if(num > 10) {
            Intent intent = new Intent(TestActivity.this, GameOver.class);
            intent.putExtra("correct", numOfCorrect);
            intent.putExtra("incorrect", numOfIncorrect);
            startActivity(intent);
          }

      }

    });
    falseButton = (Button) findViewById(R.id.false_button);
    falseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          checkAnswer(false);
          double score = (numOfCorrect / num) * 100;
          currentIndex = (currentIndex + 1) % questionBank.length;
          correctNum.setText("Correct: " + numOfCorrect);
          incorrectNum.setText("Incorrect: " + numOfIncorrect);
          avgScore.setText("Score: " + String.format("%.2f", score) + "%");
          updateQuestion();
          num++;

        }

    });

    updateQuestion();
  }
}

这是 Activity_main.xml 的布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/correct_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff00ff12" />

    <TextView
        android:id="@+id/incorrect_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffff5440" />

    <TextView
        android:id="@+id/average_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffff8c1a" />

    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp" />



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"/>

      <Button
          android:id="@+id/false_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/false_button"/>

    </LinearLayout>
</LinearLayout>

以及activity_game_over.xml 的布局:

    <LinearLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">


    <TextView
        android:id="@+id/correct_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

   <TextView
       android:id="@+id/incorrect_txt"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

    </LinearLayout>

如果我使用 setContentView(R.layout.activity_game_over); 就可以让它工作; 但是,当我尝试再次启动主要 Activity 时,它会启动 GameOver Activity ,但它应该启动 GeoQuiz Activity 。

最佳答案

GameOver Activity 中,您没有设置任何内容 View setContentView(),因此无法找到 textView,findViewById 返回 null,因此在调用 setText() 时会得到 NullPointerException。

关于java - Android 应用程序在 TextView 中启动 Intent 并使用 setText() 后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29595796/

相关文章:

Android,为监控应用程序找到一个 Intent 的发送者

java - 如何通过 key 从 JSON 中获取值?

java - 您好,我尝试将数据库中的字符串值转换为 int,但没有发生

java - Android 将 apk 与应用程序集成

android - 添加向右滑动删除 ListView 项目

android - USB 智能手机的最大输出安培数

Android - 如何在设置缩放级别和标记的 Android 应用程序中启动 Google map Intent

java - 将 XML 节点加载到 Java 对象中

java - 死锁时出现奇怪的线程转储

java - 我可以在不使用循环的情况下从Java中的单行输入读取多个整数吗