android - 是什么导致了这个 IllegalArgumentException : Service not Registered?

标签 android android-service

每当我旋转手机或按下主页按钮时,应用程序就会崩溃,并且出现以下异常:

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

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

<service android:name="StationService" />

最佳答案

我意识到我的问题是在服务最初没有绑定(bind)的时候取消绑定(bind)我的服务。只有在 onPause() 中解绑服务似乎已经解决了我的问题。

关于android - 是什么导致了这个 IllegalArgumentException : Service not Registered?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13570358/

相关文章:

java - 无法编码可序列化数据的值(value)

java - 服务启动时尝试调用位置权限但应用程序崩溃

android - 查明设备是否有软键

android - Cordova 应用程序 : does webassembly supported on Android?

android - 如何在 Bundle 中发送自定义对象的 ArrayList

android - 如何每天从上午 9 点到下午 4 点运行服务?

Android Pie - 隔夜服务一直被杀死

android - 如何以编程方式将单词添加到任何 Android 键盘预测词典中?

java - 杀死应用程序时 SharedPreferences 消失

用于标签的 Android onclicklistener