繁体   English   中英

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

[英]Android app crashes after launching intent and using setText() in TextView

我试图使用意图并在启动的活动中的TextViews中设置值来启动活动,但是当我尝试这样做时,我的应用程序崩溃了。 这是我要启动的活动。 当我尝试在最后两行代码中设置文本时会崩溃

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);
  }
}

以及即将开展的活动。 我在trueButton onClickListener方法中使用了意图:

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);我可以使它工作setContentView(R.layout.activity_game_over); 但是,当我尝试再次启动主要活动时,它将启动GameOver活动,但应改为启动GeoQuiz活动。

GameOver活动中,您未设置任何内容视图setContentView() ,因此无法找到findViewByIdfindViewById返回null ,因此在调用setText()时会收到NullPointerException。

如果我使用setContentView(R.layout.activity_game_over);我可以使它工作。 但是,当我尝试再次启动主要活动时,它将启动GameOver活动,但应改为启动GeoQuiz活动。 请帮忙!

启动时运行的活动是在项目的android清单xml文件中定义的。

寻找动作android:name =“ android.intent.action.MAIN”

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM