android - NullPointerException when using setExitTransition to Fragment -


i trying animate fragment's enter , exit. setentertransition works fine, if use setexittransition, throws nullpointerexception. if use setreturntransition, doesn't show animation.

here's have.

private void showfragment() {     fragment fragment = joingroupfragment.newinstance();     fragmenttransaction ft = getfragmentmanager().begintransaction()             .add(r.id.fragment_container, fragment, join_group_fragment_tag);       if (build.version.sdk_int >= 21) {         log.i(tag, "set enter transition");         fragment.setentertransition(new slide(gravity.top)); //      fragment.setexittransition(new slide(gravity.bottom));     }      ft.commit(); }  private void dismissfragment() {     fragment joingroupfragment = getfragmentmanager().findfragmentbytag(join_group_fragment_tag);     if (joingroupfragment != null) {         fragmenttransaction ft = getfragmentmanager().begintransaction(); //      ft.hide(joingroupfragment);         ft.remove(joingroupfragment);         ft.commit();     } } 

now, if use setexittransition, i'm getting following error.

java.lang.nullpointerexception: attempt read field 'int android.app.fragment.mcontainerid' on null object reference @ android.app.backstackrecord$1.onpredraw(backstackrecord.java:1131) @ android.view.viewtreeobserver.dispatchonpredraw(viewtreeobserver.java:944) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1970) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1061) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5885) @ android.view.choreographer$callbackrecord.run(choreographer.java:767) @ android.view.choreographer.docallbacks(choreographer.java:580) @ android.view.choreographer.doframe(choreographer.java:550) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:753) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)

i looked around , found out there's bug raised regarding this. https://code.google.com/p/android/issues/detail?id=82832

there's workaround posted. tried same thing fragmentmanager instead of supportfragmentmanager, i'm still facing same issue.

the workaround tried

// in oncreate(). getfragmentmanager().begintransaction().add(r.id.fragment_container, new fragment())                     .addtobackstack("dummy")                     .commit(); 

private void showfragment() {     fragment fragment = joingroupfragment.newinstance();     fragmenttransaction ft = getfragmentmanager().begintransaction()             .replace(r.id.fragment_container, fragment, join_group_fragment_tag);      ft.addtobackstack(null);     // ft.addtobackstack("dummy");     // ft.addtobackstack("my_backstack); // none of these work     ft.commit();       if (build.version.sdk_int >= 21) {         log.i(tag, "set enter transition");         fragment.setentertransition(new slide(gravity.top));         fragment.setexittransition(new slide(gravity.bottom));     }      ft.commit(); }  private void dismissfragment() {     fragment joingroupfragment = getfragmentmanager().findfragmentbytag(join_group_fragment_tag);     if (joingroupfragment != null) {         fragmenttransaction ft = getfragmentmanager().begintransaction(); //      ft.hide(joingroupfragment);         ft.remove(joingroupfragment);         ft.commit();     } } 

i know there aren't fragments in backstack transition, adding dummy fragment doesn't help. there workaround this?

update

instead of dummy fragment, used same fragment.

getfragmentmanager().begintransaction().add(r.id.fragment_container, joingroupfragment.newinstance())                         .addtobackstack("dummy")                         .commit(); 

private void showfragment() {     fragment fragment = joingroupfragment.newinstance();     fragmenttransaction ft = getfragmentmanager().begintransaction()             .replace(r.id.fragment_container, fragment, join_group_fragment_tag);      ft.addtobackstack("dummy");     ft.commit();       if (build.version.sdk_int >= 21) {         log.i(tag, "set enter transition");         fragment.setentertransition(new slide(gravity.top));         fragment.setexittransition(new slide(gravity.bottom));     }      ft.commit(); }  private void dismissfragment() {     fragment joingroupfragment = getfragmentmanager().findfragmentbytag(join_group_fragment_tag);     if (joingroupfragment != null) {         fragmenttransaction ft = getfragmentmanager().begintransaction(); //      ft.hide(joingroupfragment);         ft.remove(joingroupfragment);         ft.commit();     } } 

if this, when call showfragment() first time, added (in oncreate()) fragment shows exit transition , new 1 shows enter transition, when call dismissfragment(), still throws same error. have been keeping log of backstack count using getbackstackentrycount() , shows 2 when removing fragment , still the error.

but don't understand how first fragment shows exit transition.


i tried out this.

getfragmentmanager().begintransaction().add(r.id.fragment_container, new fragment())                         .addtobackstack("dummy")                         .commit(); 

private void showfragment() {     fragment fragment = joingroupfragment.newinstance();     fragmenttransaction ft = getfragmentmanager().begintransaction()             .replace(r.id.fragment_container, fragment, join_group_fragment_tag);      ft.addtobackstack("dummy");     ft.commit();       if (build.version.sdk_int >= 21) {         log.i(tag, "set enter transition");         fragment.setentertransition(new slide(gravity.top));         fragment.setexittransition(new slide(gravity.bottom));     }      ft.commit(); }  private void dismissfragment() {     fragment joingroupfragment = getfragmentmanager().findfragmentbytag(join_group_fragment_tag);     if (joingroupfragment != null) {         fragmenttransaction ft = getfragmentmanager().begintransaction();         ft.replace(r.id.fragment_container, new fragment());         ft.remove(joingroupfragment);         ft.commit();     } } 

this works , shows exit transition. backstackentrycount keeps on increasing despite of remove call increasing used memory.

so turns out not understand fragmentmanager's backstack yet. trying done without backstack. simple.

for transition, need replace fragment other fragment, otherwise not transition.

private void showfragment() {     fragment fragment = joingroupfragment.newinstance();     fragmenttransaction ft = getfragmentmanager().begintransaction()             .replace(r.id.fragment_container, fragment, join_group_fragment_tag);      ft.commit();       if (build.version.sdk_int >= 21) {         fragment.setentertransition(new slide(gravity.top));         fragment.setexittransition(new slide(gravity.bottom));     }      ft.commit(); }  private void dismissfragment() {     fragment joingroupfragment = getfragmentmanager().findfragmentbytag(join_group_fragment_tag);     if (joingroupfragment != null) {         fragmenttransaction ft = getfragmentmanager().begintransaction();         ft.replace(r.id.fragment_container, new fragment());         ft.commit();     } } 

this working well. i'll try backstack , post later.


Comments