1

您好,感谢您的支持。

我有一个Service绑定到我的MainActivity.

当我尝试在Messenger内部实例化一个新表单Service以与活动进行通信时。

Messenger mService = new Messenger(mBinder);

正是当执行到这一点时,我得到 NullPointerException。

这是服务的代码:

public class LocalService extends Service {
    public int i;
    public ArrayList<String> al;
     // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    Messenger mService = new Messenger(mBinder);
    public Task t;

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    public void sayHello() {

        // Create and send a message to the service
        Message msg = Message.obtain(null, 1, 0, 0);
        try {
            mService.send(msg);
        } catch (RemoteException e) {
             e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("", "sono nel service");
        t= new Task();
        t.execute();
        return mBinder;
    }   

这是我的 LogCat:

12-22 13:17:03.382: E/AndroidRuntime(1625): FATAL EXCEPTION: main
12-22 13:17:03.382: E/AndroidRuntime(1625): java.lang.RuntimeException: Unable to instantiate service com.example.quotes.LocalService: java.lang.NullPointerException
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2347)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.access$1600(ActivityThread.java:130)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Looper.loop(Looper.java:137)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.reflect.Method.invoke(Method.java:511)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at dalvik.system.NativeStart.main(Native Method)
12-22 13:17:03.382: E/AndroidRuntime(1625): Caused by: java.lang.NullPointerException
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Binder.queryLocalInterface(Binder.java:230)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.IMessenger$Stub.asInterface(IMessenger.java:27)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Messenger.<init>(Messenger.java:139)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at com.example.quotes.LocalService.<init>(LocalService.java:21)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.Class.newInstanceImpl(Native Method)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.Class.newInstance(Class.java:1319)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2344)
12-22 13:17:03.382: E/AndroidRuntime(1625):     ... 10 more
4

2 回答 2

0

不要在此行中发送 null 作为参数:

Message msg = Message.obtain(null, 1, 0, 0);
于 2012-12-22T13:22:16.593 回答
-1

问题解决了,我发布这个以防有人感兴趣:

Messenger 是 Parcelable 的,因此可以放入 Intent extra。调用 bindService() 的活动在 Intent 上附加了一个 Messenger 作为额外内容。服务从 Intent 中获取该 Messenger。当服务需要向 Activity 发送消息时,服务会在 Messenger 上发送消息。主Activity上的Handler通过handleMessage接收消息。

问题解决了。

于 2012-12-22T21:15:19.263 回答