Android Fragment IllegalStateException

时间:2013-10-09 14:09:06

标签: android android-fragmentactivity

以下是我正在采取的输出:

10-09 21:57:56.344: W/dalvikvm(3141): threadid=1: thread exiting with uncaught exception (group=0x40cb8378)
10-09 21:57:56.364: E/AndroidRuntime(3141): FATAL EXCEPTION: main
10-09 21:57:56.364: E/AndroidRuntime(3141): java.lang.IllegalStateException: Activity has been destroyed
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1329)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:548)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:532)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.trade.MainActivity.showFragment(MainActivity.java:85)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.trade.MainActivity.showSettingsFragment(MainActivity.java:179)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.trade.Home.setting(Home.java:40)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.trade.Home.onOptionsItemSelected(Home.java:31)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:964)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.widget.AbsListView$1.run(AbsListView.java:3533)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.os.Handler.handleCallback(Handler.java:615)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.os.Looper.loop(Looper.java:213)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at android.app.ActivityThread.main(ActivityThread.java:4787)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-09 21:57:56.364: E/AndroidRuntime(3141):     at dalvik.system.NativeStart.main(Native Method)
10-09 21:57:58.847: I/Process(3141): Sending signal. PID: 3141 SIG: 9

我不知道这个例外。请帮我理解。

由于

1 个答案:

答案 0 :(得分:0)

我会尽力回答你的一般性问题,因为这是你所要求的。如果您需要针对应用程序中特定错误的特定帮助,则需要在此处发布MainActivity(尤其是第85行附近的部分)。

你得到的是一个Android系统异常,它可以告诉你,你正在使用一个不再“活着”的Activity。这很容易实现,并且很可能与不正确的变量占用(或者在您的问题下面的评论中提到的不正确的实例占用)。不要通过{{1}自己构建Activity个实例。运算符)。一个例子可能如下

new

正如您在这个简单示例中所认识到的那样,变量import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class MyActivity extends Activity { public static Activity instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (instance != null) { instance.setContentView(R.layout.myLayout); } instance = this; finish(); startActivity(new Intent(this, MyActivity.class)); } } 仅在第一次重新 - 进入活动时被读取!但是仍有人提到旧的(现在“死”)活动。那么系统该怎么办?对这个“死”活动采取行动?不,它会给你上面看到的异常

所以简而言之:很可能你有一个instance,一个Fragment或另一个不可或缺的“操作”“死”活动的部分(操作=使用引用它或某些资源(Dialog ...)来自它)

正如我所说,您的错误可能很容易修复,请提供您的代码,我们将能够提供帮助。但也许你甚至可以使用上面的信息自己修复它 - 这对我自己肯定会更令人满意:)