代码之家  ›  专栏  ›  技术社区  ›  Robert Lewis

运行时异常:“无法销毁活动”;“服务未注册”

  •  0
  • Robert Lewis  · 技术社区  · 7 年前

    我看过其他关于这个问题的帖子,看不出我做错了什么。当我点击平板电脑上的“后退”按钮时

    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()

    0 回复  |  直到 7 年前