Java Android应用程序在启动intent并在TextView中使用setText()后崩溃
我正在尝试使用已启动活动的文本视图中的意图和设置值启动活动,但当我尝试这样做时,我的应用程序崩溃。这是我要发起的活动。当我试图在最后两行代码中设置文本时,它崩溃了Java Android应用程序在启动intent并在TextView中使用setText()后崩溃,java,android,android-intent,crash,Java,Android,Android Intent,Crash,我正在尝试使用已启动活动的文本视图中的意图和设置值启动活动,但当我尝试这样做时,我的应用程序崩溃。这是我要发起的活动。当我试图在最后两行代码中设置文本时,它崩溃了 public class GameOver extends Activity { TextView correctTxt; TextView incorrectTxt; @Override protected void onCreate(Bundle savedInstanceState) { supe
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\u game\u over),我可以让它工作代码>
但是,当我尝试再次启动主活动时,它会启动GameOver活动,但它应该启动GeoQuike活动 在GameOver
活动中,您没有设置任何内容视图setContentView()
,因此无法找到文本视图,findViewById
返回null
,因此在调用setText()
时会出现NullPointerException
如果我使用setContentView(R.layout.activity\u game\u over),我可以让它工作;但是,当我尝试再次启动主活动时,它会启动GameOver活动,但它应该启动GeoQuike活动。请帮忙
启动时运行的活动在项目的android清单xml文件中定义
寻找
action-android:name=“android.intent.action.MAIN”我试着用setContentView(R.layout.activity\u game\u over)来做这件事;当我这样做时,它会工作,但是当我尝试从主活动启动应用程序时,它只会再次启动GameOver活动。是否需要重置setContentView()?它就像是在通过intent调用GameOver活动时存储视图一样。这行代码有助于避免NPE和应用程序崩溃。首先启动的活动在应用程序清单中定义setContentView()
只需将布局与活动相关联,您无需“重置”它。那么我猜您正在设置在onCreate中进行的onClick。。。或者,您可能从未关闭gameover活动,因此当您返回appActivities时,它仍处于打开状态。在退出程序时,默认情况下保持打开状态并暂停。我从未关闭过活动。也许这就是问题所在?我不熟悉安卓系统。如何关闭它?完成();很明显,你不能把它放在onCreate或你的活动中,只要打开和关闭,你也可以发布你的清单吗。?