切换片段错误

时间:2015-09-07 07:32:48

标签: android android-fragments

try {
        FragmentManager manager = getSupportFragmentManager();

        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setCustomAnimations(android.R.anim.fade_in,
                android.R.anim.fade_out);
        transaction.replace(R.id.record_container, fragment, mark);
        transaction.commit();
        //manager.executePendingTransactions();
    } catch (Exception e) {
        Log.e("my", "replaceFrag error");
    }

当我在三个Fragment之间切换时,有时会抛出此错误:

java.lang.IllegalStateException: No host
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1179)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1991)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
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:5047)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)

Log说No host,但我不知道主机是什么?

片段是通过这种方式创建的:

if (intent.getAction().equals(Constants.P2P.RET_GET_PLAYBACK_FILES)) {
            Log.e("waitload", "waitload=" + waitload);
            if (null == rlFrag) {
                rlFrag = new RecordListFragment();
                rlFrag.setUser(contact);
            }
            String[] names = (String[]) intent
                    .getCharSequenceArrayExtra("recordList");
            list = new ArrayList<String>();
            for (String str : names) {
                list.add(str);
            }
            if (waitload > 0) {
                rlFrag.setList(list);
                isLoadingChange = false;
                madapter.setList(list);
                madapter.notifyDataSetChanged();
                replaceFrag(rlFrag, fragments[0]);
                waitload--;
                Log.e("waitload", "loaded" + waitload);
            } else {
                madapter.upLoadData(list);
                Intent it = new Intent();
                it.setAction(Constants.Action.REPEAT_LOADING_DATA);
                // it.putStringArrayListExtra("adddata", (ArrayList<String>)
                // list);
                sendBroadcast(it);
            }
        } else if (intent.getAction().equals(
                Constants.P2P.ACK_RET_GET_PLAYBACK_FILES)) {
            if (null == faultFrag) {
                faultFrag = new FaultFragment();
            }
            int result = intent.getIntExtra("result", -1);
            if (result == Constants.P2P_SET.ACK_RESULT.ACK_PWD_ERROR) {
                finish();
                T.showShort(mContext, R.string.password_error);
            } else if (result == Constants.P2P_SET.ACK_RESULT.ACK_NET_ERROR) {
                Log.e("my", "net error resend:set npc time");
                faultFrag.setErrorText(getResources().getString(
                        R.string.net_error));
                waitload = 0;
                replaceFrag(faultFrag, fragments[2]);
            } else if (result == Constants.P2P_SET.ACK_RESULT.ACK_INSUFFICIENT_PERMISSIONS) {
                finish();
                T.showShort(mContext, R.string.insufficient_permissions);
            }
        } 

4 个答案:

答案 0 :(得分:3)

在源代码FragmentManagerImpl SourceCodeLink,int方法moveToState上找到一个高峰,你会看到

if (mActivity == null && newState != Fragment.INITIALIZING) {
        throw new IllegalStateException("No activity");
}

所以,“没有主机”意味着“没有活动”,我想当你在三个片段之间切换时,会抛出一个导致你的应用程序崩溃并使主机活动为空的异常。

答案 1 :(得分:1)

我解决了我的问题) 我将动画监听器添加到片段

public class BaseFragment extends Fragment {

...
 @Override
    public Animation onCreateAnimation(final int transit, boolean enter, int nextAnim) {
        Animation animation = super.onCreateAnimation(transit, enter, nextAnim);

        if (animation == null && nextAnim != 0) {
            animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
        }

        if (animation != null) {

            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    if (mainActivity != null)
                        mainActivity.animationProcessing = true;
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if (mainActivity != null)
                        mainActivity.animationProcessing = false;

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

        }
        return animation;
    }
}

当我在MainActivity中调用replace()之后,我将检查animationProcessing

if(animationProcessing) return;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_left, R.anim.slide_right);
transaction.replace(R.id.container, curFragment);
transaction.commit();

答案 2 :(得分:0)

试试这个。

Fragment fr;
fr = new YourFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.record_container, fr);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();

这应该有用,然后你就可以制作你想要的动画了。

答案 3 :(得分:0)

在过渡期间,似乎没有片段/活动。 有时是是在动画结束前触发另一个替换。

注释掉transaction.setCustomAnimations行,你应该没事。