intent.putextra上的空指针

时间:2017-07-22 18:07:55

标签: android android-intent nullpointerexception

好吧不确定为什么它现在不起作用。使用倒数计时器运行服务。应用程序现在崩溃,每次说当Intent.putExtra为空时为空。
这是服务类

 @Override
public void onCreate() {
    super.onCreate();

    stopNotify();

    Log.i(TAG, "Starting timer...");

    cdt = new CountDownTimer(900000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {



            Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
            bi.putExtra("countdown", millisUntilFinished);
            sendBroadcast(bi);
        }

        @Override
        public void onFinish() {
            Log.i(TAG, "Timer finished");

            showNotification();

            savepref();



            Intent intent = new Intent(BroadcastService.this, MainActivity.class);


            intent.putExtra("id1",id1);

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            startActivity(intent);





        }
    };

    cdt.start();
}



@Override
public IBinder onBind(Intent intent) {

    return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
}





@Override
public void onDestroy() {
    cdt.cancel();
    Log.i(TAG, "Timer cancelled");
    super.onDestroy();
}

这里也是广播接收器以及其他相关内容:

private BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtras() != null) {

            long millisUntilFinished = intent.getLongExtra("countdown", 0);
            String secs = String.valueOf(intent.getLongExtra("countdown", 0));
            Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
                tv.setText(secs);
        }
        }

};

这里也是log cat文件

07-22 13:57:27.337 17515-17515/com.apksrc.loot E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.apksrc.loot, PID: 17515
                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, long)' on a null object reference
                                                                 at com.apksrc.loot.BroadcastService$1.onTick(BroadcastService.java:77)
                                                                 at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6836)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

1 个答案:

答案 0 :(得分:1)

这是原因。您是否定义了 bi ?:

bi.putExtra("countdown", millisUntilFinished);