我试图让我的活动与正在运行的后台服务通信,但不断收到 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);
并创建一个服务连接:
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;
}
};
然后我有一个按钮的点击监听器来调用我的服务中的方法:
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()
方法 os 被覆盖:
@Override
public IBinder onBind(Intent arg0) {
return binder;
}
我真的希望有人能解决这个问题,因为我现在完全迷路了。如果您需要更多信息,请询问。
编辑:Line 240 (NullPointerException)
这是一个:
boundServiceServer.getMessagesFromUI(messageString);
附加信息:
我在以下地址的 pastebin 上粘贴了一些代码。
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)
当我尝试向服务发送数据时,调用按钮后应用程序崩溃。