调用getSystemService();在CountDownTimer构造函数中给出NPE

时间:2017-02-13 11:45:31

标签: android nullpointerexception countdowntimer

我有一个绑定的服务类Timer。在其中,我想发起一个更新通知的新CountDownTimer

如果我尝试将IntentPendingIntentNotificationManager的声明移到onTick()方法正文之外,则会发生异常。如果在onTick()方法中声明和实例化它们,它可以正常工作。 任何人都可以帮助我理解为什么会这样,以及我如何解决它?在每次刻录工作期间创建这些对象但似乎浪费资源。

private CountDownTimer newDownTimer() {
    return new CountDownTimer(millisInFuture, countDownInterval) {

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(getApplicationContext(), com.kush.app.stayput.MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        public void onTick(long millisUntilFinished) {
            //Do something in every tick
            if (isPaused || isCanceled) {
                //If user requested to pause or cancel the count down timer
                cancel();
            } else {
                tView.setText("TEXT");
                //Put count down timer remaining time in a variable
                timeRemaining = millisUntilFinished;

                //Update notification
                NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(MainActivity.getContext())
                        .setContentTitle(getText(R.string.notification_title))
                        .setContentIntent(pendingIntent)
                        .setContentText("TEXT")
                        .setSmallIcon(R.drawable.ic_stat_name);

                mNotificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
            }
        }

        public void onFinish() {
            //Do something when count down finished
            timer = newUpTimer();
        }
    }.start();
}
FATAL EXCEPTION: main
   Process: com.example.kush.com.kush.app.stayput, PID: 16993
   java.lang.RuntimeException: Unable to instantiate service com.kush.app.stayput.countdown.Timer: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:2862)
       at android.app.ActivityThread.-wrap4(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
       at android.content.ContextWrapper.getSystemService(ContextWrapper.java:627)
       at com.kush.app.stayput.countdown.Timer$1.<init>(Timer.java:156)
       at com.kush.app.stayput.countdown.Timer.newDownTimer(Timer.java:153)
       at com.kush.app.stayput.countdown.Timer.reset(Timer.java:147)
       at com.kush.app.stayput.countdown.Timer.<init>(Timer.java:85)
       at java.lang.Class.newInstance(Native Method)
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:2859)
       at android.app.ActivityThread.-wrap4(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:148) 
       at android.app.ActivityThread.main(ActivityThread.java:5417) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

getSystemService是您的活动/服务继承的android.content.Context类的方法。 CountDownTimer但是不继承该类。

您可能必须将上下文传递给newDownTimer函数,并确保仅在创建活动/服务后调用它。