嗨,谢谢你的支持。
请为我的主要活动提供服务。
我尝试在服务中实例化一个新的Messenger表单以与活动进行通信。
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
答案 0 :(得分:0)
不要在此行中将null作为参数发送:
Message msg = Message.obtain(null, 1, 0, 0);
答案 1 :(得分:-2)
问题解决了,我发布这个以防有人可能感兴趣:
Messenger是Parcelable,因此可以放入Intent extra。调用bindService()的活动将Messenger作为额外的Intent附加。该服务从Intent获取Messenger。 当服务需要将消息发送到活动时,服务会在Messenger上发送消息。 主活动上的Handler通过handleMessage接收消息。
问题已解决。