Why Fragment crashes on .hide() with .setCustomAnimations()?

时间秒杀一切 提交于 2019-12-13 00:09:05

问题


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>

Code:

    @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();
        }
        });

                                }

The error occurs:

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)

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


回答1:


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



来源:https://stackoverflow.com/questions/51513173/why-fragment-crashes-on-hide-with-setcustomanimations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!