返回QuizActivity时App force关闭

时间:2014-01-03 13:05:50

标签: android eclipse

我正在设计一个测验应用程序。我陷入了一个非常愚蠢的步骤。当我从活动开始返回测验活动时,应用程序强制关闭。有人帮助。当我点击暂停按钮时,请使用Timeplease显示活动,但是当我按下Timeplease活动中的后退按钮时,它不会返回到Quizactivity.Also有人可以告诉如何只计算正确问题的总时间吗?

这是QuizActivity.java

public class QuizActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    timerValue = (TextView) findViewById(R.id.textView2);
    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
    resume = (Button) findViewById(R.id.resume);
    resume.setVisibility(View.INVISIBLE);
     resume.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             startTime = SystemClock.uptimeMillis();
             customHandler.postDelayed(updateTimerThread, 0);
             resume.setVisibility(View.INVISIBLE); // or View.INVISIBLE depending on what you want to do
             pause.setVisibility(View.VISIBLE);
         }
     });
     pause = (Button) findViewById(R.id.pause);
     pause.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             timeSwapBuff += timeInMilliseconds;
             customHandler.removeCallbacks(updateTimerThread);
             resume.setVisibility(View.VISIBLE); // or View.INVISIBLE depending on what you want to do
             pause.setVisibility(View.INVISIBLE);
             Intent i = new Intent(QuizActivity.this,Timeplease.class);
             startActivity(i); 
         }
     });

    Bundle b = getIntent().getExtras(); 
    qid = b.getInt("start");
    temp = qid;
    agecal = (Button) findViewById(R.id.buttoncalage);
    agecal.setOnClickListener(new View.OnClickListener()        
    {
        public void onClick(View view) 
        {
            Intent i = new Intent(QuizActivity.this,AgeCalculation.class);
            startActivity(i); 
        }
    }); 

    DbHelper db = new DbHelper(this);
    quesList = db.getAllQuestions();
    currentQ = quesList.get(qid);
    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);
    rde = (RadioButton)findViewById(R.id.radio4);
    butNext= (Button)findViewById(R.id.button1);
    setQuestionView();

    butNext.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
            if(currentQ.getanswer().equals(answer.getText()))
            {
                score++;
            }
            if(qid<(temp+20)){                  
                currentQ=quesList.get(qid);
                setQuestionView();
            }else{
                Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score);
                b.putInt("time",secs);
                //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            }
        }
    });
}
private void setQuestionView()
{
    txtQuestion.setText(currentQ.getquestion());
    rda.setText(currentQ.getopta());
    rdb.setText(currentQ.getoptb());
    rdc.setText(currentQ.getoptc());
    rdd.setText(currentQ.getoptd());
    rde.setText(currentQ.getopte());
    qid++;
}
private Runnable updateTimerThread = new Runnable() {
    public void run() {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;
        secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":" + String.format("%02d", secs) + ":"+ String.format("%03d", milliseconds));customHandler.postDelayed(this, 0);
    }
};

}

这是Timeplease.java

public class Timeplease extends Activity {
    Button back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeplease);
        addListenerOnButton();
    }
    public void addListenerOnButton()
    {
        back = (Button) findViewById(R.id.buttonback);
        back.setOnClickListener(new View.OnClickListener()      
        {
            public void onClick(View view) 
            {
                Intent i = new Intent(Timeplease.this,QuizActivity.class);
                startActivity(i); 
            }
        }); 
    }
}

这是log cat

01-03 18:57:16.712: W/dalvikvm(499): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-03 18:57:16.722: E/AndroidRuntime(499): FATAL EXCEPTION: main
01-03 18:57:16.722: E/AndroidRuntime(499): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iqtest/com.example.iqtest.QuizActivity}:     java.lang.NullPointerException
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-03 18:57:16.722: E/AndroidRuntime(499):  at  android.os.Handler.dispatchMessage(Handler.java:99)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.os.Looper.loop(Looper.java:123)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-03 18:57:16.722: E/AndroidRuntime(499):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 18:57:16.722: E/AndroidRuntime(499):  at java.lang.reflect.Method.invoke(Method.java:507)
01-03 18:57:16.722: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-03 18:57:16.722: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-03 18:57:16.722: E/AndroidRuntime(499):  at dalvik.system.NativeStart.main(Native Method)
01-03 18:57:16.722: E/AndroidRuntime(499): Caused by: java.lang.NullPointerException
01-03 18:57:16.722: E/AndroidRuntime(499):  at com.example.iqtest.QuizActivity.onCreate(QuizActivity.java:68)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 18:57:16.722: E/AndroidRuntime(499):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-03 18:57:16.722: E/AndroidRuntime(499):  ... 11 more

2 个答案:

答案 0 :(得分:1)

您在QuizActivity中使用了额外的意图但是您没有在Timeplease活动中发送任何额外内容,请尝试此操作,

Bundle b = getIntent().getExtras(); 
if(b!=null)
    qid=b.getInt("start");

答案 1 :(得分:1)

试试这段代码 -

public class Timeplease extends Activity {
    Button back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeplease);
        back = (Button) findViewById(R.id.buttonback);
        back.setOnClickListener(new View.OnClickListener()      
        {
            public void onClick(View view) 
            {
                Intent i = new Intent(Timeplease.this,QuizActivity.class);
                startActivity(i); 
                finish();
            }
        }); 
}

QuizActivity后面添加finish();代码:

Intent i = new Intent(QuizActivity.this,Timeplease.class);
startActivity(i); 
finish();

并且无需在Timeplease活动中创建额外的方法。