2012-11-26 26 views
3

每當我要麼旋轉手機或按Home鍵,應用程序崩潰,我得到以下異常:什麼導致此IllegalArgumentException:服務未註冊?

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: [email protected] 
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: [email protected] 
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" /> 

回答

1

我意識到,我的問題是,有時,當解除綁定我的服務該服務並不受限於首位。僅在onPause()中解除綁定服務似乎解決了我的問題。

0

在應用程序清單文件中註冊您的服務。

+0

服務器確實已在清單中註冊。 () – CodePrimate

0

您的服務在您的清單中註冊不正確。它要麼是:

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

如果該服務是在同一個Java包在你的清單中指定的包。

或者,如果Service是在不同的Java包,在你的清單註冊,那麼你需要指定完整的包名稱:

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

+0

我想這可能是問題所在。不過,具有.StationService,Service或com.example.package.StationService的服務無論如何都有任何影響。如果我嘗試使用該服務,則將其全部刪除將會使應用程序崩潰。 – CodePrimate

1

確保您綁定並從相同的上下文綁定。我得到這個錯誤,並發現它是因爲我以前實現了一個與我綁定我的服務的上下文封裝器。我的解綁是在View上下文中的,因此它不知道服務綁定/註冊。