RuntimeException的:无法通过暂停引发的onDestroy活动 [英] RuntimeException: Unable to pause activity triggered by onDestroy
问题描述
我每次我试图启动一个新的活动时间内得到一个例外。
I'm getting an exception every time I try to start a new activity.
03-23 10:55:17.506: E/AndroidRuntime(29210): FATAL EXCEPTION: main
03-23 10:55:17.506: E/AndroidRuntime(29210): java.lang.RuntimeException: Unable to pause activity {com.example.elan_lifestyle/com.example.elan_lifestyle.Start}: java.lang.IllegalStateException: No activity
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3046)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3001)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2979)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread.access$800(ActivityThread.java:146)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.os.Looper.loop(Looper.java:137)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread.main(ActivityThread.java:5171)
03-23 10:55:17.506: E/AndroidRuntime(29210): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 10:55:17.506: E/AndroidRuntime(29210): at java.lang.reflect.Method.invoke(Method.java:511)
03-23 10:55:17.506: E/AndroidRuntime(29210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-23 10:55:17.506: E/AndroidRuntime(29210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-23 10:55:17.506: E/AndroidRuntime(29210): at dalvik.system.NativeStart.main(Native Method)
03-23 10:55:17.506: E/AndroidRuntime(29210): Caused by: java.lang.IllegalStateException: No activity
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.support.v4.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:1898)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.support.v4.app.FragmentActivity.onPause(FragmentActivity.java:412)
03-23 10:55:17.506: E/AndroidRuntime(29210): at com.example.elan_lifestyle.Start.onPause(Start.java:172)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.Activity.performPause(Activity.java:5314)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
03-23 10:55:17.506: E/AndroidRuntime(29210): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3032)
03-23 10:55:17.506: E/AndroidRuntime(29210): ... 12 more
我已经追踪到问题源于我的onDestroy方法:
I've traced the problem to my onDestroy method:
protected void onDestroy() {
super.onDestroy();
try{
btAdapter.cancelDiscovery(); // make sure the bluetooth is not searching
}
catch(Exception e){
Log.e("start: onDestroy","btAdapter crashed");
e.printStackTrace();
}
Log.e("StartActivity","ending");
try{
unregisterReceiver(ActionFoundReceiver);
}
catch(Exception e){
Log.e("StartActivity: onDestroy","reciever crashed");
e.printStackTrace();
}
}
如果我刚开始新的活动后调用的onDestroy我得到的异常和崩溃。如果我不叫的onDestroy它工作正常,但保留原有的业务仍在运行。这里的地方的onDestroy被称为code的部分:
If I call onDestroy just after starting the new activity I get the exception and crash. If I don't call onDestroy it works fine but leaves the old activity still running. Here's the section of code where onDestroy is called:
Intent i = new Intent(Start.this, Base.class); // setup intent
i.putExtra("Address", name); // add the target device name to the intent
Start.this.startActivity(i); // start main activity
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); //override animation
onDestroy();
是什么原因造成这一异常,我该如何解决这个问题?
What is causing this Exception and how can I fix it?
推荐答案
而不是调用的onDestroy()
(你不应该过自己的第一个地方调用),应该调用完成()
来破坏你的跑步活动实例。
instead of calling onDestroy()
(which you shouldn't ever invoke by yourself at the first place), you should invoke finish()
to destroy your running activity instance.
这篇关于RuntimeException的:无法通过暂停引发的onDestroy活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!