应用程序在非常快速地单击不同的TextView时崩溃

时间:2016-07-04 13:21:33

标签: android crash textview fragment

我是android的新手,拥有一个带容器视图的Fragment(一个framelayout)。它上面有两个TextView,比如标签。每个TextView即txt1,txt2分别添加2个片段,即frgmnt1,frgmnt2。单击TextView时,将添加相应的片段。如果再次单击,则会删除片段。

那部分工作正常。但是,当我非常快速地单击TextView时,我的应用程序崩溃并显示一个" No Host"例外。谁能帮我理解为什么会这样?

(旁注,此处不需要标签布局实施)。

这是堆栈跟踪:

07-04 18:22:25.600 10971-10971/integral.com.sellfie E/AndroidRuntime: FATAL EXCEPTION: main
Process: integral.com.sellfie, PID: 10971
java.lang.IllegalStateException: No host
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1239)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2046)
    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1989)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
    at integral.com.sellfie.Fragments.MemberFragment.addFragment(MemberFragment.java:319)
    at integral.com.sellfie.Fragments.MemberFragment.showFragment(MemberFragment.java:270)
    at integral.com.sellfie.Fragments.MemberFragment$3.onClick(MemberFragment.java:242)
    at android.view.View.performClick(View.java:5207)
    at android.view.View$PerformClick.run(View.java:21168)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

1 个答案:

答案 0 :(得分:0)

希望这对您有所帮助。您只需要使用以下方法更改您的片段。

注意:这里我没有使用支持片段,因此请相应地更改代码。

 //Change fragment in appropriate container
     private void changeFragment(Fragment fragment) {
            if (fragment != 
        getFragmentManager().findFragmentById(R.id.frame_container)) {

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    //now implementing the Hack
                    if (oldFragment != null)
                        transaction.detach(oldFrag);
                    transaction.replace(R.id.frame_container, fragment);
                    transaction.attach(fragment);
                    transaction.commit();
                    oldFragment = fragment;
                }
            }