问题
Whenever I either rotate the phone or press the Home button the application crashes and I get the following exception:
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
And this is 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;
}
};
I already registered my service - At least I think I did so by inserting the following in my manifest :
<service android:name="StationService" />
回答1:
I realised my problem was unbinding my service at times when the service wasn't bound in the first place. Only unbinding the service in onPause() seems to have solved my problem.
回答2:
Be sure that you are binding and unbinding from the same context. I was getting this error and discovered it was because I had previously implemented a context wrapper with which I was binding my service. My unbind was on the View context and so it had no knowledge of the service binding/registration.
回答3:
register your service in application manifest file.
回答4:
Your service is registered incorrectly in your manifest. It should either be:
<service android:name=".StationService" />
If the service is in the same Java package as the package specified in your manifest.
Or if the Service
is in a different Java package to that registered in your manifest then you need to specify the full package name:
<service android:name="com.example.package.StationService" />
来源:https://stackoverflow.com/questions/13570358/what-is-causing-this-illegalargumentexception-service-not-registered