简体   繁体   中英

Activity has been destroyed on fragmenttransaction.commit

My project structure is as follows: I have an activity calling a fragment. My goal is to be able to simply switch fragments from 1 to another. I created a new class to handle my Clicks which is displayed below. I successfully enter my 1 case because it appears in my logcat. My issue comes after when For some reason I cannot switch Fragments. The Fragment I am trying to switch to is chatbox.

I am now trying to get the hang of fragments so any help will do. I can post my Activity class if necessary as well. Thank You.

10-08 00:43:43.706  32310-32310/com.layoutpractice E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.layoutpractice, PID: 32310
    java.lang.IllegalStateException: Activity has been destroyed
            at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1347)
            at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
            at android.app.BackStackRecord.commit(BackStackRecord.java:575)
            at com.layoutpractice.ClickityClickClick.addFragments(ClickityClickClick.java:43)
            at com.layoutpractice.ClickityClickClick.onClick(ClickityClickClick.java:33)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18482)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5097)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

Below is my onClickListener Class:

/**
 * Created by Programmer 552 on 10/7/2014.
 */
public class ClickityClickClick implements View.OnClickListener {

    public static String DEBUG = "Debug";
    private LetsPutInAChatBox activity = new LetsPutInAChatBox();
    private ChatFragment chatty = new ChatFragment();

    private int test;


    public ClickityClickClick(int test) {
        this.test = test;
    }

    @Override
    public void onClick(View v) {
        switch (test) {
            case 1:
                Log.d(DEBUG, "IN THE METHOD");
                addFragments(chatty);

        }
    }

    public void addFragments(Fragment fragment) {
        FragmentManager manager = activity.getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();
    }
}

Here is my Fragment that I am trying to call with my addFragments method:

public class ChatFragment extends Fragment {
    LetsPutInAChatBox activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        activity = new LetsPutInAChatBox();
        View rootView = inflater.inflate(R.layout.chat_box, container, false);

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = (LetsPutInAChatBox) activity;
        super.onAttach(activity);
    }

}

when you commit fragment,make sure your activity is not finishing. so check like this:

if (!isFinishing()) {
            fragmentTransaction.commit();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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