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
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