사용하려고하면 NullPointerException
이 표시됩니다. 생성자 밖으로 이동하면 잘 동작합니다. 그래서, 나는 무슨 일이 일어나고 있는지 궁금해. 여기서비스의 생성자에서 Context.getPackageManager를 사용할 수없는 이유
코드입니다 :
package com.example.nullptrservice;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
public MyService() {
super();
PackageManager pm = this.getPackageManager();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.i("MyService", "---------- started --------");
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) { return null; }
}
내가 수동으로 서비스를 실행할 때, 로그 캣 출력입니다 :
D/AndroidRuntime(4174): D/AndroidRuntime(4174): AndroidRuntime START com.android.internal.os.RuntimeInit D/AndroidRuntime(4174): CheckJNI is OFF D/AndroidRuntime(4174): Calling main entry com.android.commands.am.Am D/dalvikvm(4184): Late-enabling CheckJNI D/AndroidRuntime(4174): Shutting down VM I/ActivityManager( 148): Start proc com.example.nullptrservice for service com.example.nullptrservice/.MyService: pid=4184 uid=10066 gids={} I/AndroidRuntime(4174): NOTE: attach of thread 'Binder Thread #3' failed D/dalvikvm(4174): GC_CONCURRENT freed 103K, 81% free 489K/2560K, paused 1ms+1ms D/jdwp (4174): Got wake-up signal, bailing out of select D/dalvikvm(4174): Debugger has detached; object registry had 1 entries D/AndroidRuntime(4184): Shutting down VM W/dalvikvm(4184): threadid=1: thread exiting with uncaught exception (group=0x409ee1f8) E/AndroidRuntime(4184): FATAL EXCEPTION: main E/AndroidRuntime(4184): java.lang.RuntimeException: Unable to instantiate service com.example.nullptrservice.MyService: java.lang.NullPointerException E/AndroidRuntime(4184): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2237) E/AndroidRuntime(4184): at android.app.ActivityThread.access$1600(ActivityThread.java:123) E/AndroidRuntime(4184): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) E/AndroidRuntime(4184): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(4184): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(4184): at android.app.ActivityThread.main(ActivityThread.java:4424) E/AndroidRuntime(4184): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(4184): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(4184): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) E/AndroidRuntime(4184): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) E/AndroidRuntime(4184): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime(4184): Caused by: java.lang.NullPointerException E/AndroidRuntime(4184): at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86) E/AndroidRuntime(4184): at com.example.nullptrservice.MyService.(MyService.java:12) E/AndroidRuntime(4184): at java.lang.Class.newInstanceImpl(Native Method) E/AndroidRuntime(4184): at java.lang.Class.newInstance(Class.java:1319) E/AndroidRuntime(4184): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2234) E/AndroidRuntime(4184): ... 10 more
안드로이드 4.0.3
또한 여기 related 질문 a bunch있다 on SO
다른 관련 Q : [Activity의 생성자에서 getApplicationContext()가 null 포인터 예외를 throw하는 이유는 무엇입니까?] (http://stackoverflow.com/questions/16004891/why-getapplicationcontext-in-constructor-of-activity-throws-null -pointer-excep) –