这是什么导致了IllegalArgumentException: Service not Registered错误?

5
无论我是旋转手机还是按Home键,应用程序都会崩溃,并出现以下异常:
11-25 22:17:23.855: E/AndroidRuntime(5033): FATAL EXCEPTION: main
11-25 22:17:23.855: E/AndroidRuntime(5033): java.lang.RuntimeException: Unable to stop activity {com.liteapps.handin_3/com.liteapps.handin_3.MainActivity}: java.lang.IllegalArgumentException: Service not registered: com.liteapps.handin_3.MainActivity$2@42116cf0
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3363)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3417)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3615)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread.access$700(ActivityThread.java:142)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1214)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.os.Looper.loop(Looper.java:137)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread.main(ActivityThread.java:4931)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at java.lang.reflect.Method.invoke(Method.java:511)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at dalvik.system.NativeStart.main(Native Method)
11-25 22:17:23.855: E/AndroidRuntime(5033): Caused by: java.lang.IllegalArgumentException: Service not registered: com.liteapps.handin_3.MainActivity$2@42116cf0
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:917)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ContextImpl.unbindService(ContextImpl.java:1253)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:405)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at com.liteapps.handin_3.MainActivity.onStop(MainActivity.java:71)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1204)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.Activity.performStop(Activity.java:5146)
11-25 22:17:23.855: E/AndroidRuntime(5033):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3358)
11-25 22:17:23.855: E/AndroidRuntime(5033):     ... 12 more

这里是mConnection。
  /** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() 
{

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

我已经注册了我的服务 - 至少我认为我通过在我的清单中插入以下内容来实现:

<service android:name="StationService" />
4个回答

3

请确保您在相同的上下文中进行绑定和解绑。我曾经遇到过这个错误,后来发现是因为我之前实现了一个上下文包装器,在其中绑定了我的服务。而我的解绑是在View上下文中进行的,因此它并不知道服务绑定/注册的情况。


2

我意识到我的问题是在服务未绑定时有时会解除绑定。只有在onPause()中解除服务绑定似乎解决了我的问题。


0

在应用程序清单文件中注册您的服务。


服务器确实在清单文件中注册了。(<service android:name ="StationService"/>) - CodePrimate

0

您的清单文件中注册的服务存在错误。应该是以下之一:

<service android:name=".StationService" />

如果服务与清单文件中指定的Java包位于同一个包中。

或者,如果Service与清单文件中注册的Java包不同,则需要指定完整的包名:

<service android:name="com.example.package.StationService" />


我想这可能是问题所在。然而,无论是.StationService、Service还是com.example.package.StationService都没有任何影响。但是如果我完全删除它,尝试使用服务时应用程序将崩溃。 - CodePrimate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接