我试图通过onFinish()传递一个intent时得到一个NullPointerException

时间:2015-03-16 16:12:54

标签: android android-intent

我要做的是使用CountDownTimer类中的onFinish()方法通过intent传递值。意图流是这样的。 MyCountDownTimer类包含将传递intent / values的方法。看起来像这样。我评论了NullPointerException的来源。这是我的重试活动的片段。

 /*
  *  Retry Activity
  */

@Override
    public void onClick(View v) {
        if (v.getId()== R.id.retry){

            Bundle extras1 = getIntent().getExtras();
            whichTest = extras1.getInt("whichTest"); //NullPointerException


            if (whichTest == 1){
                Intent intent1 = new Intent(Retry.this, Test1.class);
                startActivity(intent1);
            }

            if (whichTest == 2){
                Intent intent1 = new Intent(Retry.this, Test2.class);
                startActivity(intent1);
            }

这是使用该方法的Activity。它叫做Test1

/*
  *  Test1 Activity
  */

//Timer

    textCounter = ((TextView)findViewById(R.id.textCounter));


    myCountDownTimer = new MyCountDownTimer(textCounter, 5000, 1000);
    myCountDownTimer.start();
   textCounter.setText("");
    myCountDownTimer.onTick(5000);


}

@Override
public void onClick(View v) {



            if (textCounter==null){

                myCountDownTimer.onFinish();

    }

此代码段来自MyCountDownTimer类

/*
  *  MyCountDownTimer
  */
 @Override
    public void onFinish() {


        Intent retryIntent = new Intent(textCounter.getContext(), Retry.class);

           if (textCounter.getContext().equals(Test1.class)){
               whichTest = 1;
               retryIntent.putExtra("whichTest",whichTest);
           }
        if (textCounter.getContext().equals(Test2.class)){
            whichTest = 2;
            retryIntent.putExtra("whichTest",whichTest);
        }
        textCounter.getContext().startActivity(retryIntent);



}

如果您想查看更完整的版本,请转到此处https://gist.github.com/asonofman

这是logcat

03-16 12:32:39.379    4625-4625/com.dose.apps.brainnoodles E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.dose.apps.brainnoodles.Retry.onClick(Retry.java:40)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18796)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5455)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

最有可能的是:

textCounter.getContext().equals(Test1.class)

将始终返回false。您有机会寻找:

textCounter.getContext() instanceof Test1

在此之后,您的意图不会添加任何额外内容,因此:

Bundle extras1 = getIntent().getExtras();

extras1将为null