nullpointerexception当活动试图与有界服务进行通信时

时间:2014-03-17 19:33:43

标签: android nullpointerexception android-service android-service-binding

我正在尝试让我的活动与正在运行的后台服务进行通信,但不断得到一个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;
        }
    };

然后我有一个按钮的点击监听器来调用我的服务中的方法:

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);

其他信息:

我在下面的地址上粘贴了一些关于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)

当我尝试将数据发送到服务时,应用程序在调用按钮后崩溃。

1 个答案:

答案 0 :(得分:0)

我自己找到了一个解决方案。我已在pastebin上发布了我的活动和服务代码。

活动: http://pastebin.com/jMgraK2r

ServerService: http://pastebin.com/yquZAi2i

由于某种原因,从活动到服务的信使对象以某种方式将自己设置为null。 通过将其声明为静态,问题得以解决。

我希望任何人都可以从中学到任何东西。 感谢上面的所有回复。