我不知道是什么导致我的应用程序崩溃,logcat 说它从 MAIN 获得了一个致命的异常,我真的不知道这意味着我对这些东西很陌生。发生的情况是第一个活动加载正常,但是一旦我按下按钮加载第二个活动就会停止。抱歉,如果它的问题太长或已经被问到,我已经尝试了很多以前问过的问题。
显现:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".QuizActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:exported="false"
>
<intent-filter>
<category android:name="android.intent.category.DEFAULT">
</category>
</intent-filter>
</activity>
<activity
android:name=".MainMenu"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
第一项活动
public class MainMenu extends Activity {
Button quizButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mainmenu);
quizButton = (Button)findViewById(R.id.launchQuiz);
quizButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == quizButton.getId()) {
Intent launchQuizActivity = new Intent(MainMenu.this, QuizActivity.class);
MainMenu.this.startActivity(launchQuizActivity);
}
}
});
}
}
这是第二个:
public class QuizActivity extends Activity {
Intent returnHome = new Intent(QuizActivity.this, matt.apps.quiz.MainMenu.class);
String questions[] = {"What month is it?","What OS is this?" };
String lChoices[] = {"July","IOS"};
String cChoices[] = {"August","Android"};
String rChoices[] = {"October", "Windows?"};
int answers[] = {R.id.lButton,R.id.cButton };
boolean correct = false;
int numCorrect = 0;
int numWrong = 0;
int btnClicked;
int i = -1;
int t = 0;
Button lButton;
Button cButton;
Button rButton;
Button nextButton;
TextView questionTV;
TextView greetingTV;
public boolean getCorrect(int id) {
if (id == answers[i]) {
correct = true;
} else {
correct = false;
}
return correct;
}
OnClickListener clicker = new OnClickListener() {
//TODO change to method to handle answers
@Override
public void onClick(View v) {
btnClicked = v.getId();
if (btnClicked == R.id.nextButton) { // next button
i += 1;
if (i <= questions.length) {
questionTV.setText(questions[i]);
lButton.setText(lChoices[i]);
cButton.setText(cChoices[i]);
rButton.setText(rChoices[i]);
nextButton.setText("Next question");
} else {
questionTV.setText("You got " + numCorrect + " right and " + numWrong + " wrong.");
lButton.setText("");
cButton.setText("");
rButton.setText("");
} // end of next button
} else { // answer buttons
if (i > -1) {
if (i <= questions.length) {
if (getCorrect(btnClicked) == true) {
numCorrect += 1;
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
} else {
numWrong += 1;
Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
}
}
}
}// end of answer buttons
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
greetingTV = (TextView)findViewById(R.id.greetingTV);
questionTV = (TextView)findViewById(R.id.questionTV);
lButton = (Button)findViewById(R.id.lButton);
cButton = (Button)findViewById(R.id.cButton);
rButton = (Button)findViewById(R.id.rButton);
nextButton = (Button)findViewById(R.id.nextButton);
//set on click listeners for buttons
questionTV.setText("Please click the start button to begin.");
nextButton.setText("Start");
}
}
错误日志:
E/AndroidRuntime( 620): FATAL EXCEPTION: main
E/AndroidRuntime( 620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{matt.apps.quiz/matt.apps.quiz.QuizActivity}: java.lang.NullPointerException
E/AndroidRuntime( 620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
E/AndroidRuntime( 620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime( 620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime( 620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)