我正试图使我的活动与运行的后台服务通信,但不断获得NullPointerException,这没有任何意义。至少对我来说是这样。希望别人能找出问题所在。
首先是我的LogCat输出:
03-17 20:16:56.390: E/AndroidRuntime(11563): FATAL EXCEPTION: main
03-17 20:16:56.390: E/AndroidRuntime(11563): java.lang.NullPointerException
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.takenumber.TakeNumber.sendMessageToService(TakeNumber.java:240)
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.takenumber.TakeNumber$2.onClick(TakeNumber.java:334)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.view.View.performClick(View.java:4204)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.view.View$PerformClick.run(View.java:17355)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.os.Handler.handleCallback(Handler.java:725)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.os.Looper.loop(Looper.java:137)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-17 20:16:56.390: E/AndroidRuntime(11563): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 20:16:56.390: E/AndroidRuntime(11563): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-17 20:16:56.390: E/AndroidRuntime(11563): at dalvik.system.NativeStart.main(Native Method)
我在活动中做了什么:
表示启动服务并绑定它:
serviceIntent = new Intent(context, ServerService.class);
serviceIntent.putExtra("messageType", StaticValues.MESSAGE_START_SERVICE);
serviceIntent.putExtra("devicetype", StaticValues.DEVICE_TAKENUMBER);
serviceIntent.putExtra("MESSENGER", new Messenger(messageHandler));
startService(serviceIntent);
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
并创建一个serviceConnection:
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (isServer) {
boundServiceServer = ((ServerService.CustomBinder) service).getService();
} else {
boundServiceClient = ((ClientService.CustomBinder) service).getService();
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
boundServiceClient = null;
boundServiceServer = null;
}
};
然后我有一个click listener用于按钮调用我的服务中的一个方法:
categoryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button buttonClicked = (Button)v;
int categoryID = v.getId();
String categoryTitle = buttonClicked.getText().toString();
// TODO: Change to send only to showline devices
boundServiceServer.getMessagesFromUI(StaticValues.MESSAGE_NUMBER_TAKEN + StaticValues.MESSAGE_DELIMITER + categoryID + StaticValues.MESSAGE_DELIMITER + categoryTitle);
}
});
在我的服务中,我这样做:创建一个" binder
":
public class CustomBinder extends Binder {
public ServerService getService() {
return ServerService.this;
}
}
我的服务中的方法getMessagesFromUI()
(从活动中接收字符串)简单地看起来像这样:
public void getMessagesFromUI(String message) {
// DO stuff
}
当然onBind()
方法被覆盖了:
@Override
public IBinder onBind(Intent arg0) {
return binder;
}
我真的希望有人能解决这个问题,因为我现在完全迷路了。如果你需要更多的信息,尽管问。
编辑:Line 240 (NullPointerException)
是这个:
boundServiceServer.getMessagesFromUI(messageString);
附加信息:
我已经把我的一些代码粘贴到以下地址。
TakeNumber活动在这里:http://pastebin.com/DxrWfEYM
和ServerService在这里:http://pastebin.com/7QjiQbTi
我还在代码中插入了"syso",以了解发生了什么。下面是logcat输出:
03-18 14:23:05.740: D/dalvikvm(20260): GC_CONCURRENT freed 213K, 16% free 4276K/5064K, paused 2ms+3ms, total 24ms
03-18 14:23:05.790: I/System.out(20260): server connecting
03-18 14:23:05.810: I/System.out(20260): onBind
03-18 14:23:05.830: D/libEGL(20260): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
03-18 14:23:05.840: D/libEGL(20260): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
03-18 14:23:05.840: D/libEGL(20260): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
03-18 14:23:05.890: D/OpenGLRenderer(20260): Enabling debug mode 0
03-18 14:23:05.910: I/System.out(20260): server binding
03-18 14:23:05.910: I/System.out(20260): CustomBinder
03-18 14:23:13.710: I/System.out(20260): trying to send to server
03-18 14:23:13.720: D/AndroidRuntime(20260): Shutting down VM
03-18 14:23:13.720: W/dalvikvm(20260): threadid=1: thread exiting with uncaught exception (group=0x41954930)
03-18 14:23:13.720: E/AndroidRuntime(20260): FATAL EXCEPTION: main
03-18 14:23:13.720: E/AndroidRuntime(20260): java.lang.NullPointerException
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.takenumber.TakeNumber.sendMessageToService(TakeNumber.java:244)
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.takenumber.TakeNumber$2.onClick(TakeNumber.java:339)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.view.View.performClick(View.java:4204)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.view.View$PerformClick.run(View.java:17355)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.os.Handler.handleCallback(Handler.java:725)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.os.Looper.loop(Looper.java:137)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 14:23:13.720: E/AndroidRuntime(20260): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 14:23:13.720: E/AndroidRuntime(20260): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-18 14:23:13.720: E/AndroidRuntime(20260): at dalvik.system.NativeStart.main(Native Method)
在按钮被调用后,当我试图向服务发送数据时,应用程序崩溃了。
我自己想出了一个解决办法。我已经在pastebin上发布了我的活动和服务代码。
活动:http://pastebin.com/jMgraK2r
ServerService:http://pastebin.com/yquZAi2i
由于某种原因,从活动到服务的信使对象以某种方式将自身设置为null。通过将其声明为静态,问题就解决了。
我希望任何人都能从中学到一些东西。感谢以上所有的回复。