我查看了有关此问题的其他帖子,看不到我在做什么错。当我在平板电脑上单击“后退”按钮时,我得到
java.lang.RuntimeException: Unable to destroy activity {...TestActivity}: java.lang.IllegalArgumentException: Service not registered: ...TestActivity@1035a3d1
导致错误的行在此处为unbindService()
:
@Override
protected void onDestroy( ) {
super.onDestroy( ); // does putting this line first fix the "can't destroy activity; service is not registered" crash? No.
if( serviceBound ) {
getApplicationContext().unbindService( this ); // ('this' is ServiceConnection implementation)
serviceBound = false;
} else {
if( DEBUG ) Log.d( TAG, "onDestroy() found that ThermocoupleService was not bound" );
}
onStopDisposables.clear(); // because apparently onStop() isn't always called
}
IllegalArgumentException
似乎是指this
,这是我要取消绑定的活动。它实现了ServiceConnection
接口。
该服务在onStart()
中的绑定方式如下:
bindThermoServiceIntent = new Intent( getApplicationContext(), ThermocoupleService.class );
if( !( serviceBound = bindService( bindThermoServiceIntent, /*ServiceConnection interface*/ this, Context.BIND_AUTO_CREATE ) ) )
throw new RuntimeException( TAG + ": bindService() call in onStart() failed" );
serviceComponentName = getApplicationContext().startService( bindThermoServiceIntent ); // bind to it AND start it
if( DEBUG ) Log.d( TAG, "Service running with ComponentName " + serviceComponentName.toShortString() );
除此问题外,“活动和服务”似乎工作正常。服务使用startForeground()
来保持运行。