为什么我不能在Service的构造函数中使用Context.getPackageManager

时间:2013-08-14 09:34:45

标签: android

如果我尝试使用它,我会得到NullPointerException。如果移出构造函数,它可以正常工作。所以,我很好奇发生了什么。

这是代码:

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; }
}

当我手动运行服务时,这是logcat输出:

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

Android 4.0.3

此处有a bunchrelated个问题在此

2 个答案:

答案 0 :(得分:15)

在创建/初始化对象时,您无法使用Service(或Activity作为Context

相反,覆盖onCreate()并在那里进行初始化并删除显式构造函数。

答案 1 :(得分:-1)

尝试使用处理程序从onCreate()或服务构造函数中取出,并在处理程序中完成工作。