简体   繁体   English

选项卡片段错误发生,并且片段父活动已被破坏

[英]Tab fragment error occurring and fragment parent activity has been destroyed

在此处输入图片说明

Error occurs when switching from one tab to another, the Main Fragment Activity has been destroyed. 从一个选项卡切换到另一个选项卡时,主片段活动已被破坏,会发生错误。

My fragment child Profile_album_details code: 我的片段子Profile_album_details代码:

case R.id.ib_photo_delete:
        String stringiq = "";
        NewTask.iq = new CustomIQ("<albummanagement xmlns='naseebalbum'           action='deletepicture'><albumname>"+album.getAlbum_name()+"</albumname><picturetitle>"+picture.getTitle()+"</picturetitle></albummanagement>");
        NewTask.iq.setType(IQ.Type.SET);
        new NewTask(context).execute();
        while(NewTask.connection.isAuthenticated() == false){}
        Profile_album_details.pictures.remove(imageno);
        Profile_display_photo.pictures = Profile_album_details.pictures;
        new MainActivity().updateTab(new Profile_album_details(), R.id.tab2,true); 

Main activity parent fragment class: 主要活动父片段类:

public void updateTab(Fragment frg,
            int place_holder, boolean addTobackStack) 
    {
        // TODO Auto-generated method stub
        FragmentTransaction ft=getFragmentManager().beginTransaction();

        ft.replace(place_holder, frg);
//      ft.setTransition(transaction);

        if(addTobackStack)
            ft.addToBackStack(null);
            ft.commit();
    }



> 10-02 15:25:55.061: E/AndroidRuntime(1920): FATAL EXCEPTION: main
>     10-02 15:25:55.061: E/AndroidRuntime(1920): java.lang.IllegalStateException: Activity has been destroyed
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1333)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.app.BackStackRecord.commit(BackStackRecord.java:574)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at com.example.tabs.MainActivity.updateTab(MainActivity.java:166)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at com.example.tabs.MainActivity.launchNewFragment(MainActivity.java:175)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at adapters.Profile_display_photo.onClick(Profile_display_photo.java:164)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.view.View.performClick(View.java:4240)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.view.View$PerformClick.run(View.java:17721)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.os.Handler.handleCallback(Handler.java:730)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.os.Handler.dispatchMessage(Handler.java:92)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.os.Looper.loop(Looper.java:137)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at android.app.ActivityThread.main(ActivityThread.java:5103)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at java.lang.reflect.Method.invokeNative(Native Method)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at java.lang.reflect.Method.invoke(Method.java:525)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
>     10-02 15:25:55.061: E/AndroidRuntime(1920):   at dalvik.system.NativeStart.main(Native Method)

I think I understand the question now. 我想我现在明白了这个问题。 FragmentTransaction.commit() is not executed immediately, there is a delay between when the method is called and when the actual transaction commit is executed. FragmentTransaction.commit()不会立即执行,在调用该方法与执行实际事务提交之间存在延迟。 That's the reason why when you call the FragmentTransaction.commit() and then finish() the Activity you get that error, because the Activity is destroyed before the transaction commit is executed. 这就是为什么当您调用FragmentTransaction.commit()然后finish() Activity会收到该错误的原因,因为该Activity在执行事务提交之前已被销毁。

To avoid this use FragmentManager.executePendingTransactions() . 为了避免这种情况,请使用FragmentManager.executePendingTransactions() This should, as the name indicates, execute all the pending transactions immediately. 顾名思义,这应立即执行所有挂起的事务。 Without seeing your whole code I would guess that the best place is before the Activity.finish() call, but if that doesn't work then try after the commit() 没有看到完整的代码,我想最好的地方是在Activity.finish()调用之前,但是如果不起作用,请尝试在commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM