为什么Fragment使用.setCustomAnimations()在.hide()上崩溃?

时间:2018-07-25 07:28:55

标签: android fragment android-animation

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),就不会发生错误

1 个答案:

答案 0 :(得分:1)

如developer.android.com上所述,“通过在布局XML文件中定义片段将片段添加到活动布局时,您将无法在运行时删除该片段。如果您打算在和在用户交互过程中,您必须在活动首次开始时将片段添加到活动中。”“ link

尝试动态添加片段。 how to