按下onBackPressed()时“java.lang.IllegalStateException:Activity已被销毁”

时间:2014-01-24 11:56:04

标签: java android exception android-fragments illegalstateexception

我有一个带有动画片段的活动。当我按下后退以关闭活动时,我收到此错误。这是什么意思?

  01-24 12:41:24.407: E/AndroidRuntime(23621):    FATAL EXCEPTION: main
 01-24 12:41:24.407: E/AndroidRuntime(23621):     java.lang.IllegalStateException: Activity has been destroyed
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1351)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.BackStackRecord.commit(BackStackRecord.java:574)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at com.eni.enim4s.TranslucentActivity$1.run(TranslucentActivity.java:70)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.os.Handler.handleCallback(Handler.java:725)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.os.Handler.dispatchMessage(Handler.java:92)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.os.Looper.loop(Looper.java:176)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.ActivityThread.main(ActivityThread.java:5279)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at java.lang.reflect.Method.invokeNative(Native Method)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at java.lang.reflect.Method.invoke(Method.java:511)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
 01-24 12:41:24.407: E/AndroidRuntime(23621):   at dalvik.system.NativeStart.main(Native Method)

我的代码:

  public class TranslucentActivity extends Activity {

private Fragment fragment;
private FrameLayout frame;
private android.app.FragmentTransaction ft;
private android.app.FragmentManager fragmentManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.translucent);

     frame =(FrameLayout)findViewById(R.id.container);
  }

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
        frame.postDelayed(new Runnable() {

        @Override
            public void run() {
                // TODO Auto-generated method stub
         fragment = new ViewDetail();
         fragmentManager = getFragmentManager(); 
         ft = fragmentManager.beginTransaction(); 
        ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
        ft.addToBackStack(null); 
        ft.replace(R.id.container, fragment, "Dettaglio"); 
        ft.commit();//the error is here


            }
        }, 500);
}




 @Override
     public void onBackPressed() {
    super.onBackPressed();
    this.finish();
   }

}

3 个答案:

答案 0 :(得分:4)

行上没有发生错误

this.finish();//the error is here

最有可能发生在

ft.commit();

当您看到此错误时,此代码会在活动被销毁后运行,因此没有FragmentManager或任何Activity状态可供使用with,因此交易无法提交。

这是因为您在延迟后运行此代码,并且如果您按下窗口焦点更改并且任务已安排,但活动在任务实际开始之前被销毁。

你可以做的是在任务中运行UI更改之前添加if (!isFinishing())检查,但我建议你重新考虑一下你的UI流程。 如果您希望事务在延迟之后但在应用完成之前运行,请将finish()指令移至任务结束run()(无论是是否具有良好的用户体验UI。

另请注意,您不必同时执行这两项操作

super.onBackPressed();
this.finish();//the error is here
onBackPressed()中的

:要么执行一些自定义操作(例如完成应用)要替换默认行为,将其保留为原始版本( super)行为。

答案 1 :(得分:2)

你可以删除这个onBackPressed,因为android隐式地按下后退键完成你的活动。

不要拨打addToBackStack

ft.addToBackStack(null); 

删除上面这一行,这会在backstack中添加你的片段事务,然后按下后退键,第一个事务被恢复,然后活动结束。

如果您将其删除,请按完成活动。

更改为:

    if(hasFocus){
    frame.postDelayed(new Runnable() {

    @Override
        public void run() {
            // TODO Auto-generated method stub
     fragment = new ViewDetail();
     fragmentManager = getFragmentManager(); 
     ft = fragmentManager.beginTransaction(); 
    ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
    ft.addToBackStack(null); 
    ft.replace(R.id.container, fragment, "Dettaglio"); 
    ft.commit();//the error is here


        }
    }, 500);
  }

答案 2 :(得分:-1)

@Override
     public void onBackPressed() {
      super.onBackPressed() 

   }

删除此行this.finish() ;

实际上super.onBackPressed()完成了您的活动