为什么Fragment使用.setCustomAnimations()在.hide()上崩溃? [英] Why Fragment crashes on .hide() with .setCustomAnimations()?

查看:422
本文介绍了为什么Fragment使用.setCustomAnimations()在.hide()上崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xml:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">



     <fragment
                android:id="@+id/the_fragment"
                android:name="com.example.MyFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                tools:layout="@layout/fragment_my"/>

      <Button 
                android:id = "@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Show/Hide"/>

    </RelativeLayout>

代码:

    @Override
                public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                     super.onViewCreated(view, savedInstanceState);
                     mMyFragment = (MyFragment) getChildFragmentManager()
                                        .findFragmentById(R.id.the_fragment);
                        ((Button)view.findViewById(R.id.button)).setOnClickListener(view->{
        if (...){
        //hide
        getChildFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.slide_in_from_bottom, R.animator.slide_out_to_bottom)
                                        .hide(mMyFragment)
                                        .commit();

        }else{
        //show
        getChildFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.slide_in_from_bottom, R.animator.slide_out_to_bottom)//in this line the app crash
                                        .show(mMyFragment)
                                        .commit();
        }
        });

                                }

发生错误:

Process: by.gramophone.develop, PID: 21508
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.startViewTransition(android.view.View)' on a null object reference
        at android.support.v4.app.FragmentManagerImpl.completeShowHideFragment(FragmentManager.java:1700)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1797)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:792)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

更新: 但是,如果我删除.setCustomAnimations(R.animator.slide_in_from_bottom, R.animator.slide_out_to_bottom),就不会发生错误

UPDATE: But if I remove .setCustomAnimations(R.animator.slide_in_from_bottom, R.animator.slide_out_to_bottom) the error doesn't occurs

推荐答案

developer.android.com上说,通过在布局XML文件中定义片段将片段添加到活动布局时,您无法删除如果您打算在用户交互过程中切入和切出片段,则必须在活动首次启动时将片段添加到活动中."

As it is said on developer.android.com "When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts..." link

尝试动态添加片段. 如何

Try adding the fragment dynamically. how to

这篇关于为什么Fragment使用.setCustomAnimations()在.hide()上崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆