IllegalArgumentException:绑定服务时,connection为null

时间:2013-04-30 01:18:16

标签: android android-service

我正在使用我从github获得的标签主机用于我的项目,它最近破了。这只发生在我加载包含我的谷歌地图的标签并且适用于所有其他标签的情况下。我收到错误IllegalArgumentException:调用

时连接为null
// Bind the location service
Intent testIntent = new Intent(this, wd_locationService.class);
bindService(testIntent, myConnection, Context.BIND_AUTO_CREATE);

这是堆栈跟踪:

08-21 17:25:48.524: E/AndroidRuntime(20354): FATAL EXCEPTION: main
08-21 17:25:48.524: E/AndroidRuntime(20354): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.backtrailtech.hunterelite/com.backtrailtech.hunterelite.wd_mapView}: java.lang.IllegalArgumentException: connection is null
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at com.backtrailtech.hunterelite.ScrollableTabActivity.startGroupActivity(ScrollableTabActivity.java:288)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at com.backtrailtech.hunterelite.ScrollableTabActivity.onCheckedChanged(ScrollableTabActivity.java:272)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.RadioGroup.access$600(RadioGroup.java:54)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.CompoundButton.setChecked(CompoundButton.java:129)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.CompoundButton.toggle(CompoundButton.java:87)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.RadioButton.toggle(RadioButton.java:76)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.widget.CompoundButton.performClick(CompoundButton.java:99)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.view.View$PerformClick.run(View.java:17355)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.os.Handler.handleCallback(Handler.java:725)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.os.Looper.loop(Looper.java:137)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.ActivityThread.main(ActivityThread.java:5041)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at java.lang.reflect.Method.invoke(Method.java:511)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at dalvik.system.NativeStart.main(Native Method)
08-21 17:25:48.524: E/AndroidRuntime(20354): Caused by: java.lang.IllegalArgumentException: connection is null
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.ContextImpl.bindService(ContextImpl.java:1415)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.ContextImpl.bindService(ContextImpl.java:1407)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at com.backtrailtech.hunterelite.wd_mapView.onCreate(wd_mapView.java:1007)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.Activity.performCreate(Activity.java:5104)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-21 17:25:48.524: E/AndroidRuntime(20354):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-21 17:25:48.524: E/AndroidRuntime(20354):    ... 22 more

另外,到目前为止,我只在Galaxy Nexus 4.2.2上看到这个问题而且我没有在模拟器上看到。感谢。

1 个答案:

答案 0 :(得分:3)

事实证明这是由我传递给bindService()的空连接(myConnection)引起的。这不会导致运行android 2.1的模拟器出现问题,但在我的手机上运行android 4.2.2确实如此。似乎新版Android中的一些变化导致了这个问题。

通过创建myConnection的新实例而不仅仅是声明它来修复它。

myConnection = new ServiceConnection(){

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }
};