2017-02-13 74 views
0

我有一個綁定的服務類Timer。其中我想發起一個新的更新通知的CountDownTimer調用getSystemService();在CountDownTimer構造函數中給出NPE

如果我嘗試將Intent,PendingIntentNotificationManager的聲明移至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)  
+0

這看起來像一個lifecylce bug.Context尚不存在。你有可能在一個構造函數中創建CountDownTimer?或者在調用Activty.onCreate()之前的其他地方? – muetzenflo

+0

只有一個活動存在,MainActivity.class。它存在,因爲它是從服務啓動相同的上下文。在返回CountDownTimer中也沒有代碼;無論如何,調用該設置環境。 – KUSH42

+0

嘗試使用:'this.getSystemService' – rafsanahmad007

回答

0

getSystemService是在android.content.Context類的活動/服務繼承的方法。然而,CountDownTimer不會繼承那個類。

您可能必須將上下文傳遞給您的newDownTimer函數,並確保您只在創建活動/服務後調用它。

+1

Android OS在調用MainActivity的onCreate()之前與服務類交互;方法。這會在嘗試執行系統調用時導致NPE,因爲上下文尚不存在。因爲我在服務的構造函數中間接調用了方法,所以它受到了這個影響。 我通過將方法從構造函數移動到服務類的onStartCommand()方法中解決了我的問題。 – KUSH42