활동 및 서비스가있는 애플리케이션이 있습니다.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));
}
}
});
}
}
내 질문은 :
- 것이 가능 세트 업이 다른 스레드에서 실행하는 서비스가 아니라 UI 스레드입니까?
- 그렇지 않은 경우, 활동이 종료 되었더라도 배경이 제대로 작동 할 필요가 있습니다.이 "무언가"가 존재합니까? 나는 서비스가 실행하지만, 계속 활동을 닫으면
내가 예상대로 서비스 작품을 만드는 방법, 매번 나는
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)
답장을 보내 주셔서 감사합니다. 알림 관리자와 다른 관리자가 매번 사용하는 null과 다른지 확인하는 것이 좋습니다. 모든 작업이 높은 작업을 수행하는 경우 모든 서비스가 UI를 중지시킬 수있는 문서를 읽었으며 서비스 내에서 스레드를 시작할 수 있습니까? 감사! – Trxplz0
내 애플리케이션에서 어디에서나 null을 확인하고 Google Play의 앱은 견고하며 크래시 보고서를받지 못했습니다. 좋은 습관입니다. – Merlevede
* 모든 서비스가 자신의 작업이 높으면 * UI를 멈출 수 있습니다 * 내게 벨소리를 울리지 않는다 ... 당신의 서비스가 죽을 수도, 예! 문서의 START_STICKY 플래그를 확인하십시오. – Merlevede