您好,感谢您的支持。
我有一个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