2014-03-07 46 views
0

我有一個應用程序的活動和服務。Android服務銷燬NullPointerException

 public class ClsService extends Service { 
     private NotificationManager notificationManager; 

     @Override 
     public IBinder onBind(Intent intent) { 
      return null; 
     } 

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

      this.notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

      // other stuffs working great! 
     } 

     @Override 
     public void onDestroy() { 
      this.notificationManager.cancelAll(); 

      super.onDestroy(); 
     } 
    } 

    public class MyActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.mainactivity); 

     CheckBox serviceEnabler = (CheckBox)findViewById(R.id.serviceEnabler); 
     serviceEnabler.setChecked(true); 
     serviceEnabler.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) { 
        startService(new Intent(this, MainService.class)); 
       } else { 
        stopService(new Intent(this, MainService.class)); 
       } 
      } 
     }); 
    } 
} 

我的問題是:

  1. 這有可能設立這個服務在其他線程中運行,而不是UI線程?
  2. 如果沒有,即使活動已關閉,我仍然需要一些全面的後臺工作,但這個「某些東西」存在,它有可能嗎?
  3. 我創造預期的服務的工作方式,如果我關閉該服務繼續運行,但活動每次我嘗試然後停止活動,我得到NullPointerExceptionthis.notificationManager.cancelAll();

    java.lang.RuntimeException: Unable to stop service [email protected]: java.lang.NullPointerException 
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3090) 
    at android.app.ActivityThread.access$3700(ActivityThread.java:125) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:4627) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:521) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
    at com.idt.march.ClsService.onDestroy(MainService.java:155) 
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3076) 
    at android.app.ActivityThread.access$3700(ActivityThread.java:125) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:4627) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:521) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
    at dalvik.system.NativeStart.main(Native Method) 
    

回答

1

你不能在另一個線程上運行服務,但可以在服務中創建一個新線程。該線程的創建方式與您在「活動」中的完全相同。

關於你的NullPointerException嘗試添加一個檢查

if (this.notificationManager == null) 
    this.notificationManager.cancelAll(); 

雖然你可能會重新考慮使用cancelAll(),因爲根據該文件,它會清除所有通知。

+0

感謝您的回答,它可能會很好的檢查通知管理器和其他管理器與我每次使用時的空值是否不同?我讀過文檔說,如果所有服務的工作量都很高,所有服務都可以停止UI,那麼我可以在服務中啓動一個線程?謝謝! – Trxplz0

+0

在我的應用程序中,我檢查了各處的空值,並且我在Google Play上的應用程序非常穩定,我還沒有收到任何崩潰報告。這是一個很好的做法。 – Merlevede

+0

*如果所有服務的工作都很高,所有服務都可以停止用戶界面*不會對我敲響鐘......您的服務可能會被殺死,是的!檢查文檔中的START_STICKY標誌。 – Merlevede

0

如果要在後臺線程中運行服務,請考慮使用IntentService。它與Service不完全相同,但它適用於您希望服務執行的工作類型。