2014-03-07 3 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. 내가 예상대로 서비스 작품을 만드는 방법, 매번 나는 this.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을 얻을 활동을 중지 한 후 시도 다른 스레드에서 서비스를 실행하지만 서비스 내에 새 스레드를 만들 수 있습니다. 쓰레드는 Activity에서와 똑같은 방법으로 생성됩니다.

당신의 NullPointerException이 당신이 문서에 따르면,이 모든 알림을 지우 때문에 cancelAll()을 사용하여 재고 수도 있지만 수표를

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

를 추가하려고에 대해서.

+0

답장을 보내 주셔서 감사합니다. 알림 관리자와 다른 관리자가 매번 사용하는 null과 다른지 확인하는 것이 좋습니다. 모든 작업이 높은 작업을 수행하는 경우 모든 서비스가 UI를 중지시킬 수있는 문서를 읽었으며 서비스 내에서 스레드를 시작할 수 있습니까? 감사! – Trxplz0

+0

내 애플리케이션에서 어디에서나 null을 확인하고 Google Play의 앱은 견고하며 크래시 보고서를받지 못했습니다. 좋은 습관입니다. – Merlevede

+0

* 모든 서비스가 자신의 작업이 높으면 * UI를 멈출 수 있습니다 * 내게 벨소리를 울리지 않는다 ... 당신의 서비스가 죽을 수도, 예! 문서의 START_STICKY 플래그를 확인하십시오. – Merlevede

0

백그라운드 스레드에서 서비스를 실행하려면 IntentService 사용을 고려하십시오. 서비스와 정확히 동일하지는 않지만 서비스에서 수행하고자하는 종류의 작업에 적합한 경우 유용합니다.