我调用bindService()时的NullPointer

时间:2012-06-19 16:10:36

标签: android nullpointerexception android-service

我正在处理一个应用程序。为简单起见,它有这样的设计:

ServiceManager< --------> CommService ------->通过AsyncTask做事......

我正在尝试将ServiceManager绑定到CommService。所以,CommService有一个Binder。我有一个NullPointerException,我不知道如何解决它。我认为ServiceConnection不能正常工作,并且绑定不活动。

此处的ServiceManager代码:

public class ProbeManagerService extends Service implements ICommManager, ICommListener {
    private CommService mService;
    private boolean mBound;

    private ServiceConnection svc1 = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder rawBinder) {
        CommBinder commBinder = (CommBinder) rawBinder;
        mService = commBinder.getService();
        mBound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        //commBinder = null;
        mBound = false;
    }
    };

    public void onCreate() {
        Log.i("ProbeManagerService, onCreate", "passage dans le onCreate du Service");
        super.onCreate();
        Intent bindIntent = new Intent(this, CommService.class);
        //startService(bindIntent); changes nothing with or without this line
        bindService(bindIntent, svc1, Context.BIND_AUTO_CREATE);
   }

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


    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ProbeManagerService, onStartCommand", "passage dans le onStartCommand du Service");
        this.getActions();
        return START_STICKY;
    }


    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void getActions() {
        if(mBound)
            this.mService.getActions(this, urlGetActions, jsonGetActions, GET_ACTIONS);
        else
            System.out.println("On n'est pasl lié");

    }

CommService代码:

public class CommService extends Service implements IComm {

private final CommBinder binder = new CommBinder(this);
private HttpClient client;


public CommService() {
    super();
}

public void onCreate() {
    super.onCreate();
    this.client = new DefaultHttpClient();
}

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

public void onDestroy() {
    super.onDestroy();
    this.client.getConnectionManager().shutdown();
}

// ============ METHODES A IMPLEMENTER DE L'INTERFACE IComm ==============
@Override
public void getActions(ICommListener listener, String url, String json, String type) {
    new SendHttpRequest(listener).execute(url, json, type);

}

CommBinder代码:

public class CommBinder extends Binder {

private CommService service;

public CommBinder(CommService service) {
    this.service = service;
}

public CommService getService() {
    return this.service;
}

Logcat:

06-19 18:03:35.665: I/ProbeManagerService, onCreate(25705): passage dans le onCreate du Service
06-19 18:03:35.775: I/System.out(25705): fichier lus
06-19 18:03:35.785: I/ProbeManagerService, onStartCommand(25705): passage dans le onStartCommand du Service
06-19 18:03:35.815: D/AndroidRuntime(25705): Shutting down VM
06-19 18:03:35.815: W/dalvikvm(25705): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-19 18:03:35.855: E/AndroidRuntime(25705): FATAL EXCEPTION: main
06-19 18:03:35.855: E/AndroidRuntime(25705): java.lang.RuntimeException: Unable to start service .probecontroller.android.services.ProbeManagerService@40516da8 with Intent { cmp=.probecontroller.android.controller/.probecontroller.android.services.ProbeManagerService }: java.lang.NullPointerException
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.app.ActivityThread.access$2800(ActivityThread.java:117)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:994)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.os.Looper.loop(Looper.java:123)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at java.lang.reflect.Method.invoke(Method.java:507)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at dalvik.system.NativeStart.main(Native Method)
06-19 18:03:35.855: E/AndroidRuntime(25705): Caused by: java.lang.NullPointerException
06-19 18:03:35.855: E/AndroidRuntime(25705):    at .probecontroller.android.services.ProbeManagerService.getActions(ProbeManagerService.java:162)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at .probecontroller.android.services.ProbeManagerService.onStartCommand(ProbeManagerService.java:124)
06-19 18:03:35.855: E/AndroidRuntime(25705):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2039)
06-19 18:03:35.855: E/AndroidRuntime(25705):    ... 10 more
06-19 18:03:51.226: I/Process(25705): Sending signal. PID: 25705 SIG: 9

欢迎任何帮助。已经花了好几个小时研究这个问题,我读了很多论坛,帖子,根本没什么用。

1 个答案:

答案 0 :(得分:4)

在服务绑定之前,您正在getActions()上调用ProbeManagerService,显然,因为null唯一可能是mServicebindService()是异步的;当该方法返回时,你将不会受到约束。