错误的Logcat详细信息:
06-24 10:23:48.836: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-24 10:23:48.865: E/AndroidRuntime(276): FATAL EXCEPTION: main
06-24 10:23:48.865: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triviality/com.example.triviality.InfoActivity}: java.lang.NullPointerException
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-24 10:23:48.865: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:23:48.865: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
06-24 10:23:48.865: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-24 10:23:48.865: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-24 10:23:48.865: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
06-24 10:23:48.865: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
06-24 10:23:48.865: E/AndroidRuntime(276): at com.example.triviality.InfoActivity.onCreate(InfoActivity.java:32)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-24 10:23:48.865: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-24 10:23:48.865: E/AndroidRuntime(276): ... 11 more
06-24 10:23:59.235: I/Process(276): Sending signal. PID: 276 SIG: 9
有两个类,QuizActivity和InfoActivity。 如果Category值为2,那么我需要显示QuizActivity而不是2,然后我需要显示InfoActivity。所以我在两个程序中都基于类别调用QuizActivity,InfoActivity。
QuizActivity.java
package com.example.triviality;
import java.util.List;
import com.example.triviality.R.raw;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.media.MediaPlayer;
public class QuizActivity extends Activity {
List<Question> quesList;
int score=0;
public static int qid=0;
Question currentQ;
TextView txtQuestion,txtQuestionnum, txtPoints,txtExplanation;
RadioButton rda, rdb, rdc, rdd;
public static int Qno=0;
Button butNext;
public MediaPlayer mpr,mpw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestionnum=(TextView)findViewById(R.id.textView2);
txtPoints=(TextView)findViewById(R.id.textView3);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
butNext=(Button)findViewById(R.id.button1);
mpr = MediaPlayer.create(QuizActivity.this, raw.right);
mpw = MediaPlayer.create(QuizActivity.this, raw.wrong);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentQ.getCATEGORY().equals("2"))
{
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
callanswer("Right");
score= score + 5;
Log.d("score", "Your score"+score);
}
else
{
score= score - 2;
callanswer("Wrong");
}
}
else
{
Intent intent = new Intent(QuizActivity.this, InfoActivity.class);
startActivity(intent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
public void callanswer(String Status)
{
txtPoints.setText("Points: " + score);
String messageVal = "Correct Answer is : ";
if (Status.equals("Right"))
{
mpr.start();
messageVal = "Answer is : ";
}
else
{
mpw.start();
}
messageVal = messageVal + currentQ.getANSWER()+"\n";
messageVal = messageVal + "More Info: " + currentQ.getDETAIL();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle(" ");
if (Status.equals("Right"))
{
alertDialog.setIcon(R.drawable.right);
}
else
{
alertDialog.setIcon(R.drawable.wrong);
}
alertDialog.setMessage(messageVal);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
if(Qno<10){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
alertDialog.show();
}
private void setQuestionView()
{
if (currentQ.getCATEGORY().equals("2"))
{
setContentView(R.layout.activity_quiz);
txtQuestionnum.setText("Question No: "+ ++Qno);
txtPoints.setText("Points: " + score);
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
qid++;
}
else
{
Intent intent = new Intent(this, InfoActivity.class);
startActivity(intent);
}
}
}
InfoActivity.java
package com.example.triviality;
import java.util.List;
import com.example.triviality.R.raw;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.media.MediaPlayer;
public class InfoActivity extends Activity {
List<Question> quesList;
int score=0;
Question currentQ;
TextView txtQuestion,txtQuestionnum, txtPoints,txtExplanation;
RadioButton rda, rdb, rdc, rdd;
public static int Qno=0;
Button butNext;
public MediaPlayer mpr,mpw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
currentQ=quesList.get(QuizActivity.qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
txtExplanation=(TextView)findViewById(R.id.textView6);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(InfoActivity.this, QuizActivity.class);
startActivity(intent);
}
});
}
private void setQuestionView()
{
Log.d("category",currentQ.getCATEGORY());
if (currentQ.getCATEGORY().equals("2"))
{
Intent intent = new Intent(InfoActivity.this, QuizActivity.class);
startActivity(intent);
}
else
{
txtQuestion.setText(currentQ.getQUESTION());
txtExplanation.setText(currentQ.getCATEGORY());
QuizActivity.qid++;
}
}
}
答案 0 :(得分:1)
你宣布了这个
List<Question> quesList;
在您currentQ=quesList.get(QuizActivity.qid)
导致NullPointerException
的任何地方和时间都没有初始化。即使您初始化列表仍然没有填充项目。
注意:不要将带有不同布局的setContentView设置为相同的活动。这不是一个好主意。每个活动都有自己的ui和自己的生命周期。