android - RuntimeException: "无法销毁活动"; “服务未注册”
问题描述
我已经查看了有关此问题的其他帖子,但看不出我做错了什么。当我点击平板电脑上的“返回”按钮时,我得到
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()
保持自身运行。
解决方案
推荐阅读
- qt - qDebug 函数 char FirstDriveFromMask( ULONG unitmask )
- c# - Use DbModelBuilder in .NET Core to use specify naming conventions
- ios - 如何在 iOS 中从 geoPDF 获取位置信息
- r - 嵌套列表的元素明智连接
- javascript - 类型错误:g[b] 未定义
- mysql - mysql table with varchar primary key field
- asp.net-core - How can I bind a comma separated list in a URL to an array of objects?
- docker - pushing docker image to registery of docker on a different host machine
- django - 查询数据库后在 Python 模型对象中进行过滤和分组
- r - 用数字、字符和特殊符号拆分复杂的字符串