Infeasible
Status Update
Comments
ne...@gmail.com <ne...@gmail.com> #2
Some classes (such as ViewPager) would have to be modified to allow setting the new child FragmentManager. Trying to do it now.
ne...@gmail.com <ne...@gmail.com> #3
Seems the issue here is that the Fragment is getting dispatchDestroy() in the first place. As from the documentation, if you do FragmentTransaction.addToBackStack(), the fragment should not be destroyed but should rather be managed by the backstack. The FragmentManagerImpl does check for whether the fragment is in the backstack.
final boolean inactive = !fragment.isInBackStack();
if (!fragment.mDetached || inactive) {
... remove fragment ...
}
However FragmentTransaction.replace() does not detach the fragment first but removes it straight away. This means the !fragment.mDetached is true and the fragment gets completely removed.
I've found a workaround (fix?) by doing
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2)
.addToBackStack(null).commit();
final boolean inactive = !fragment.isInBackStack();
if (!fragment.mDetached || inactive) {
... remove fragment ...
}
However FragmentTransaction.replace() does not detach the fragment first but removes it straight away. This means the !fragment.mDetached is true and the fragment gets completely removed.
I've found a workaround (fix?) by doing
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2)
.addToBackStack(null).commit();
al...@gmail.com <al...@gmail.com> #4
I have similar issue but it fails on findViewById:
Caused by: java.lang.IllegalStateException: Fragment does not have a view
at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1871)
at android.support.v4.app.Fragment.performResume(Fragment.java:1509)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1871)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:455)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:444)
View cannot be found because parent fragment that contains placeholder viewgroup was detached.
Caused by: java.lang.IllegalStateException: Fragment does not have a view
at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1871)
at android.support.v4.app.Fragment.performResume(Fragment.java:1509)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1871)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:455)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:444)
View cannot be found because parent fragment that contains placeholder viewgroup was detached.
mc...@gmail.com <mc...@gmail.com> #5
[Comment deleted]
mc...@gmail.com <mc...@gmail.com> #6
I have the same problem.
My Activity has FragmentViewPager with 3 Frgments.
One of those fragments - Settings - has inner container - LinearLayout and tabs on action bar. I have a few other fragments which I show only in Settings then user switching tabs.
Problem happened when I push back so that my activity get hidden - I see desktop and then I run app again and get java.lang.IllegalStateException: No activity
Offered workaround doesn't work for me :(
My Activity has FragmentViewPager with 3 Frgments.
One of those fragments - Settings - has inner container - LinearLayout and tabs on action bar. I have a few other fragments which I show only in Settings then user switching tabs.
Problem happened when I push back so that my activity get hidden - I see desktop and then I run app again and get java.lang.IllegalStateException: No activity
Offered workaround doesn't work for me :(
ma...@gmail.com <ma...@gmail.com> #7
I was having this problem but I changed my project to work with viewpagerindicator and now it's ok!
an...@gmail.com <an...@gmail.com> #8
Having the same issue. The suggested work around doesn't quite work either. Might have to instantiate the fragment every time, which kinda sucks.
lu...@gmail.com <lu...@gmail.com> #9
I had to do it that way too. Yep, it sucks, hope your fragment is not as
heavy as mine :(
El 30/04/2013 22:50, <android@googlecode.com> escribi�:
heavy as mine :(
El 30/04/2013 22:50, <android@googlecode.com> escribi�:
fe...@gmail.com <fe...@gmail.com> #10
Having the same issue here, workaround doesnt work. Will have to also reinstantiate the fragments every time :(
th...@gmail.com <th...@gmail.com> #11
For anyone interested, this is the workaround I used when I hit this exception:
public class CustomFragment extends Fragment {
private static final Field sChildFragmentManagerField;
static {
Field f = null;
try {
f = Fragment.class.getDeclaredField("mChildFragmentManager");
f.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.e(LOGTAG, "Error getting mChildFragmentManager field", e);
}
sChildFragmentManagerField = f;
}
@Override
public void onDetach() {
super.onDetach();
if (sChildFragmentManagerField != null) {
try {
sChildFragmentManagerField.set(this, null);
} catch (Exception e) {
Log.e(LOGTAG, "Error setting mChildFragmentManager field", e);
}
}
}
...
}
I generally don't like resorting to reflection to modify behavior of the Android API, but I believe this is relatively safe since you must package the v4 support library with your app (and should thus be immune from any device-specific implementation changes).
public class CustomFragment extends Fragment {
private static final Field sChildFragmentManagerField;
static {
Field f = null;
try {
f = Fragment.class.getDeclaredField("mChildFragmentManager");
f.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.e(LOGTAG, "Error getting mChildFragmentManager field", e);
}
sChildFragmentManagerField = f;
}
@Override
public void onDetach() {
super.onDetach();
if (sChildFragmentManagerField != null) {
try {
sChildFragmentManagerField.set(this, null);
} catch (Exception e) {
Log.e(LOGTAG, "Error setting mChildFragmentManager field", e);
}
}
}
...
}
I generally don't like resorting to reflection to modify behavior of the Android API, but I believe this is relatively safe since you must package the v4 support library with your app (and should thus be immune from any device-specific implementation changes).
ko...@gmail.com <ko...@gmail.com> #12
After hours of search - thanks alot android - the solution above worked for me.
What the f*** though, you call replace and it doesn't detach the existing fragment. I dunno man Android is flakey as it gets.
What the f*** though, you call replace and it doesn't detach the existing fragment. I dunno man Android is flakey as it gets.
re...@gmail.com <re...@gmail.com> #13
[Comment deleted]
re...@gmail.com <re...@gmail.com> #14
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2)
.addToBackStack(null).commit();
Solved my issue...............
.addToBackStack(null).commit();
Solved my issue...............
[Deleted User] <[Deleted User]> #15
Post #10 cleanly solved the issue for me with a simple cut and paste! (hours lost as well) Thanks theBN...!
an...@gmail.com <an...@gmail.com> #16
Unfortunatly that didnt work for me. I have a view pager inside a fragment. This view pager has 3 more fragments. Every time I change the "Parent fragment" with another, and then go back, the view pager appears empty, It does not recreate the child fragments
al...@aldoborrero.com <al...@aldoborrero.com> #17
[Comment deleted]
al...@aldoborrero.com <al...@aldoborrero.com> #18
#2 and #13 proposed solution worked for me (and I also have a view pager inside a fragment). Though I had to remove the call to addToBackStack(null).
to...@gmail.com <to...@gmail.com> #19
Hello, about #2 and #13. I dont understand where to use it.
What is mFragment1?
What is mFragment2?
How i accesss mFragment1?
How i accesss mFragment2?
What R.id.main means?
When in my code i should use it?
Im trying to use pageviewer in dialogfragment and got the same error here, thanks.
What is mFragment1?
What is mFragment2?
How i accesss mFragment1?
How i accesss mFragment2?
What R.id.main means?
When in my code i should use it?
Im trying to use pageviewer in dialogfragment and got the same error here, thanks.
na...@gmail.com <na...@gmail.com> #20
[Comment deleted]
ko...@gmail.com <ko...@gmail.com> #21
Modified the earlier code snippet for the onDestroy in a Fragment... You can still hold a reference and call replace().
@Override
public void onDetach() {
super.onDetach();
try {
Field field = Fragment.class.getDeclaredField("mChildFragmentManager");
field.setAccessible(true);
field.set(this, null);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onDetach() {
super.onDetach();
try {
Field field = Fragment.class.getDeclaredField("mChildFragmentManager");
field.setAccessible(true);
field.set(this, null);
}catch (Exception e){
e.printStackTrace();
}
}
fr...@gmail.com <fr...@gmail.com> #22
Just for others who might be looking at this bug. After looking at the code for the FragmentTabHost to see how they switch the tabbed fragments, I see that they do a ft.detach for the old fragment and an ft.attach of the new fragment. You don't see ft.replace anywhere. So doing the same thing in my activity logic now destroys the childFragmentManager properly and the viewpager fragments work as expected when you do the switch.
Here is the code I use for switching
YourFragment fragments[]=new YourFragment[5];
YourFragment currentFragment=null;
void switchFragments(int id){
FragmentManager fm = getSupportFragmentManager();
if(currentFragment!=null){
fm.beginTransaction().detach(currentFragment).commit();
}
if(fragments[id]==null){
fragments[id]=new YourFragment();
//If your fragment has not been added yet then first add it.
fm.beginTransaction().add(R.id.fragment_content, fragments[id],tabNames[id]).commit();
//Now attach the new fragment
fm.beginTransaction().attach(fragments[id]).commit();
//Update the current fragment reference
currentFragment=fragments[id];
}
else{
fm.beginTransaction().attach(fragments[id]).commit();
currentFragment=fragments[id];
}
}
Here is the code I use for switching
YourFragment fragments[]=new YourFragment[5];
YourFragment currentFragment=null;
void switchFragments(int id){
FragmentManager fm = getSupportFragmentManager();
if(currentFragment!=null){
fm.beginTransaction().detach(currentFragment).commit();
}
if(fragments[id]==null){
fragments[id]=new YourFragment();
//If your fragment has not been added yet then first add it.
fm.beginTransaction().add(R.id.fragment_content, fragments[id],tabNames[id]).commit();
//Now attach the new fragment
fm.beginTransaction().attach(fragments[id]).commit();
//Update the current fragment reference
currentFragment=fragments[id];
}
else{
fm.beginTransaction().attach(fragments[id]).commit();
currentFragment=fragments[id];
}
}
bo...@gmail.com <bo...@gmail.com> #23
#21 helped in my case (but with one addition):
I have a bunch of fragments, which I swap with a help of NavigationDrawer inside my NavigationActivity. But one of my fragments has ViewPager and after returning to this fragment it was recreated, with empty savedInstanceState. So I was forced to create two methods, which I use in switchFragments() method.
public void saveFragmentState(Fragment fragment, int contentId) {
if (fragment == null)
return;
savedStates.put(contentId, getSupportFragmentManager().saveFragmentInstanceState(fragment));
}
/**
* This method restores fragment state even if it was already added to activity
*/
public void restoreFragmentState(NavigationFragment fragment, int contentId) {
Fragment.SavedState savedFragmentState = savedStates.get(contentId);
if (fragment != null && savedFragmentState != null){
try {
Field field_mSavedFragmentState = Fragment.class.getDeclaredField("mSavedFragmentState");
field_mSavedFragmentState.setAccessible(true);
Field field_mState = savedFragmentState.getClass().getDeclaredField("mState");
field_mState.setAccessible(true);
field_mSavedFragmentState.set(fragment, field_mState.get(savedFragmentState));
}catch (Exception e){
e.printStackTrace();
}
}
}
I have a bunch of fragments, which I swap with a help of NavigationDrawer inside my NavigationActivity. But one of my fragments has ViewPager and after returning to this fragment it was recreated, with empty savedInstanceState. So I was forced to create two methods, which I use in switchFragments() method.
public void saveFragmentState(Fragment fragment, int contentId) {
if (fragment == null)
return;
savedStates.put(contentId, getSupportFragmentManager().saveFragmentInstanceState(fragment));
}
/**
* This method restores fragment state even if it was already added to activity
*/
public void restoreFragmentState(NavigationFragment fragment, int contentId) {
Fragment.SavedState savedFragmentState = savedStates.get(contentId);
if (fragment != null && savedFragmentState != null){
try {
Field field_mSavedFragmentState = Fragment.class.getDeclaredField("mSavedFragmentState");
field_mSavedFragmentState.setAccessible(true);
Field field_mState = savedFragmentState.getClass().getDeclaredField("mState");
field_mState.setAccessible(true);
field_mSavedFragmentState.set(fragment, field_mState.get(savedFragmentState));
}catch (Exception e){
e.printStackTrace();
}
}
}
tk...@google.com <tk...@google.com>
gr...@gmail.com <gr...@gmail.com> #24
Anyone know if this has been fixed in the latest release of the Support Library? (19.1.0 March 2014)
bc...@gmail.com <bc...@gmail.com> #25
It's not fixed in support library version 19.1.0 afaik. I get the same stack trace using the newest version.
ne...@gmail.com <ne...@gmail.com> #26
Uh, crap! I have a MapFragment inside a fragment. I do not programmatically create the MapFragment its in XML and gets inflated. but Android does not clean it up. I tried all the above work arounds and nothing solved my issue.
Anyone seen this with a MapFragment inside a fragment? I.E. put a MapFragment in a Navigation Drawer, switch away from map then back to the map and you will see the it throw an error on inflate
Anyone seen this with a MapFragment inside a fragment? I.E. put a MapFragment in a Navigation Drawer, switch away from map then back to the map and you will see the it throw an error on inflate
ar...@gmail.com <ar...@gmail.com> #27
I faced this problem with NavigationDrawer, steps to reproduce:
One of your content fragments should display DialogFragment, then you should replace this content fragment with another and then replace to the fragment with DialogFragment again you'll got this exception "No activity".
SOLUTION: do not hold the reference to content fragment with DialogFragment, recreate content fragment instance, yes you'll lost savedInstatnceState, but it is better than app crash.
One of your content fragments should display DialogFragment, then you should replace this content fragment with another and then replace to the fragment with DialogFragment again you'll got this exception "No activity".
SOLUTION: do not hold the reference to content fragment with DialogFragment, recreate content fragment instance, yes you'll lost savedInstatnceState, but it is better than app crash.
lo...@gmail.com <lo...@gmail.com> #28
I am also facing this problem:
I have a FragmentTabHost with nested fragments inside.
I have a data sync task which needs to clear and recreate all the tabs.
When doing { myTabHost.clearAllTabs(); } and then adding new tabs, it doesn't refresh the current tab (until I choose next tab and come back).
I tried removing all the fragments using getChildFragmentManager() and the tabSpec tag. The tab I am on goes blank, it allows me to go to other tabs, but crashes when I go back to the first one with java.lang.IllegalStateException: No activity.
Any ideas?
I have a FragmentTabHost with nested fragments inside.
I have a data sync task which needs to clear and recreate all the tabs.
When doing { myTabHost.clearAllTabs(); } and then adding new tabs, it doesn't refresh the current tab (until I choose next tab and come back).
I tried removing all the fragments using getChildFragmentManager() and the tabSpec tag. The tab I am on goes blank, it allows me to go to other tabs, but crashes when I go back to the first one with java.lang.IllegalStateException: No activity.
Any ideas?
ob...@gmail.com <ob...@gmail.com> #29
I tried to set the child FragmentManager to null as #10 or #20 suggested. It resulted in a NullPointerException in getFragment when the ViewPager was restoring its state. Maybe this was introduced in a new version of the support library? I tried both 19.0 and 19.1. The solution in #2 worked for me.
My setup is a Fragment containing a ViewPager with Fragments.
My setup is a Fragment containing a ViewPager with Fragments.
pa...@gmail.com <pa...@gmail.com> #30
to #29: when onDetach() is called on the fragment containing the ViewPager You can set the adapter of that viewpager to null. I have not tried it yet but I think it will work as there will not be any adapter for the viewpager to save its instance and restore it.
ob...@gmail.com <ob...@gmail.com> #31
No, I tried that, didn't help.
pa...@gmail.com <pa...@gmail.com> #32
I found the solution of this problem. Actually a workaround or some can call hack.
I looked at the source of FragmentStatePagerAdapter and copied the whole code in a new class that extends PagerAdapter inside my package. In restoreState(Parcelable,ClassLoader) function I surrounded the statement causing null pointer exception inside try-catch block like this.
public void restoreState(Parcelable state, ClassLoader loader) {
Bundle bundle;
if (state != null) {
bundle = (Bundle) state;
bundle.setClassLoader(loader);
Parcelable[] fss = bundle.getParcelableArray("states");
this.mSavedState.clear();
this.mFragments.clear();
if (fss != null) {
for (int i = 0; i < fss.length; i++) {
this.mSavedState.add((Fragment.SavedState) fss[i]);
}
}
Set<String> keys = bundle.keySet();
for (String key : keys)
if (key.startsWith("f")) {
int index = Integer.parseInt(key.substring(1));
Fragment f;
try {
f = this.mFragmentManager.getFragment(bundle, key);//causing problem.
// So surrounded with a try catch block.
} catch (Exception e) {
f = null;
}
if (f != null) {
while (this.mFragments.size() <= index) {
this.mFragments.add(null);
}
f.setMenuVisibility(false);
this.mFragments.set(index, f);
} else {
Log.w("FragmentStatePagerAdapter",
"Bad fragment at key " + key);
}
}
}
The only workaround to this problem was that the FragmentManagerImp return null in that line but it sends an exception as reported by the author of this Thread.
I am having 2 Horizontal ViewPager inside a Verticle ViewPager.
This trick solved my problem.
I am also attaching the file you can use instead of making a new class. Just add your Package name at the start of the file.
I looked at the source of FragmentStatePagerAdapter and copied the whole code in a new class that extends PagerAdapter inside my package. In restoreState(Parcelable,ClassLoader) function I surrounded the statement causing null pointer exception inside try-catch block like this.
public void restoreState(Parcelable state, ClassLoader loader) {
Bundle bundle;
if (state != null) {
bundle = (Bundle) state;
bundle.setClassLoader(loader);
Parcelable[] fss = bundle.getParcelableArray("states");
this.mSavedState.clear();
this.mFragments.clear();
if (fss != null) {
for (int i = 0; i < fss.length; i++) {
this.mSavedState.add((Fragment.SavedState) fss[i]);
}
}
Set<String> keys = bundle.keySet();
for (String key : keys)
if (key.startsWith("f")) {
int index = Integer.parseInt(key.substring(1));
Fragment f;
try {
f = this.mFragmentManager.getFragment(bundle, key);//causing problem.
// So surrounded with a try catch block.
} catch (Exception e) {
f = null;
}
if (f != null) {
while (this.mFragments.size() <= index) {
this.mFragments.add(null);
}
f.setMenuVisibility(false);
this.mFragments.set(index, f);
} else {
Log.w("FragmentStatePagerAdapter",
"Bad fragment at key " + key);
}
}
}
The only workaround to this problem was that the FragmentManagerImp return null in that line but it sends an exception as reported by the author of this Thread.
I am having 2 Horizontal ViewPager inside a Verticle ViewPager.
This trick solved my problem.
I am also attaching the file you can use instead of making a new class. Just add your Package name at the start of the file.
[Deleted User] <[Deleted User]> #33
thank you very much guys
i ws also having the same problem
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit(); >>>> it rely worked for me
i ws also having the same problem
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit(); >>>> it rely worked for me
da...@neosperience.com <da...@neosperience.com> #34
[Comment deleted]
da...@gmail.com <da...@gmail.com> #35
Situation:
Activity that open up a DialogFragment.
The DialogFragment contains a ViewPager with other Fragments.
The ViewPagerAdapter receive a ChildFragmentManager for obvious reasons.
Opening the DialogFragment the first time works.
Closing (back) and reopening it raise the Illegal State: No Activity exception.
Applying the reflection hack to manually set to null the child fragment manager on detach cause another error to raise:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:575)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281)
at android.view.View.dispatchRestoreInstanceState(View.java:13265)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2716)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2722)
at android.view.View.restoreHierarchyState(View.java:13243)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:447)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:960)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
The snipped of code interested in the FragmentManager class (From support):
@Override
public Fragment getFragment(Bundle bundle, String key) {
int index = bundle.getInt(key, -1);
if (index == -1) {
return null;
}
if (index >= mActive.size()) {
throwException(new IllegalStateException("Fragement no longer exists for key "
+ key + ": index " + index));
}
Fragment f = mActive.get(index);
if (f == null) {
throwException(new IllegalStateException("Fragement no longer exists for key "
+ key + ": index " + index));
}
return f;
}
The null pointer happens at first use of mActive: mActive.size()
I've no idea on how to fix it.
I think I'm going to re-instantiate the dialog fragment every time and manually resync it's state :/
As this is a FutureRelease can someone by Google give us some hint on how we can workaround the framework to make it work in the mean while?
Thank you.
(sorry for repost - I previously injected non-english text in this, by mistake)
Activity that open up a DialogFragment.
The DialogFragment contains a ViewPager with other Fragments.
The ViewPagerAdapter receive a ChildFragmentManager for obvious reasons.
Opening the DialogFragment the first time works.
Closing (back) and reopening it raise the Illegal State: No Activity exception.
Applying the reflection hack to manually set to null the child fragment manager on detach cause another error to raise:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:575)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281)
at android.view.View.dispatchRestoreInstanceState(View.java:13265)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2716)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2722)
at android.view.View.restoreHierarchyState(View.java:13243)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:447)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:960)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
The snipped of code interested in the FragmentManager class (From support):
@Override
public Fragment getFragment(Bundle bundle, String key) {
int index = bundle.getInt(key, -1);
if (index == -1) {
return null;
}
if (index >= mActive.size()) {
throwException(new IllegalStateException("Fragement no longer exists for key "
+ key + ": index " + index));
}
Fragment f = mActive.get(index);
if (f == null) {
throwException(new IllegalStateException("Fragement no longer exists for key "
+ key + ": index " + index));
}
return f;
}
The null pointer happens at first use of mActive: mActive.size()
I've no idea on how to fix it.
I think I'm going to re-instantiate the dialog fragment every time and manually resync it's state :/
As this is a FutureRelease can someone by Google give us some hint on how we can workaround the framework to make it work in the mean while?
Thank you.
(sorry for repost - I previously injected non-english text in this, by mistake)
sv...@gmail.com <sv...@gmail.com> #36
#32 helped me! Thank you!
Only things to do:
- extend every Fragment with the support v4 one(best practice)
- download the class and copy to your project
- use that class instead of FragmentStatePagerAdapter
Only things to do:
- extend every Fragment with the support v4 one(best practice)
- download the class and copy to your project
- use that class instead of FragmentStatePagerAdapter
yo...@gmail.com <yo...@gmail.com> #37
Be aware that #32 doesn't actually solve the problem. Instead it just wraps the error in a Try Catch statement. This gives an effect that the Fragments inside ViewPager don't get its restored state, so every time onCreate is called the savedInstanceState is null. I wonder how to solve this.
fr...@gmail.com <fr...@gmail.com> #39
Not entirely sure, but I seem to have ran into this issue as well. The workarounds mentioned here worked for my use-case (thanks!).
If this is indeed the same issue and if Fragment got a new version in Lollipop, then I wonder why this issue hasn't been patched. Does a fix cause problems for other applications?
If this is indeed the same issue and if Fragment got a new version in Lollipop, then I wonder why this issue hasn't been patched. Does a fix cause problems for other applications?
na...@xicom.biz <na...@xicom.biz> #40
I am also getting the related issue having crash log:
0 java.lang.NullPointerException
1 at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1190)
2 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
3 at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1233)
4 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:709)
5 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
6 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
7 at android.os.Handler.handleCallback(Handler.java:733)
8 at android.os.Handler.dispatchMessage(Handler.java:95)
9 at android.os.Looper.loop(Looper.java:136)
10 at android.app.ActivityThread.main(ActivityThread.java:5120)
11 at java.lang.reflect.Method.invokeNative(Native Method)
12 at java.lang.reflect.Method.invoke(Method.java:515)
13 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
14 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
15 at dalvik.system.NativeStart.main(Native Method)
I don't know where in the project this crash is happening. Please help!!
0 java.lang.NullPointerException
1 at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1190)
2 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
3 at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1233)
4 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:709)
5 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
6 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
7 at android.os.Handler.handleCallback(Handler.java:733)
8 at android.os.Handler.dispatchMessage(Handler.java:95)
9 at android.os.Looper.loop(Looper.java:136)
10 at android.app.ActivityThread.main(ActivityThread.java:5120)
11 at java.lang.reflect.Method.invokeNative(Native Method)
12 at java.lang.reflect.Method.invoke(Method.java:515)
13 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
14 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
15 at dalvik.system.NativeStart.main(Native Method)
I don't know where in the project this crash is happening. Please help!!
lb...@gmail.com <lb...@gmail.com> #41
@40 My workaround:
add check for savedInstanceState. if it's not null, create a new instance for each of the viewPager fragments and use them instead of what you had, and restore their states to the ones you had before (not using the savedInstantState, but use your own methods and fields).
add check for savedInstanceState. if it's not null, create a new instance for each of the viewPager fragments and use them instead of what you had, and restore their states to the ones you had before (not using the savedInstantState, but use your own methods and fields).
ke...@gmail.com <ke...@gmail.com> #42
#10 worked for me. The bug was not occuring on Lolliloop but happening with ealier versions.
Thanks
Thanks
jo...@jacksonengineering.net <jo...@jacksonengineering.net> #43
I encountered this on lollipop while switching between fragments quickly.
09-08 10:45:40.787 5950-5950/com.mycompany.BusScan E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.BusScan, PID: 5950
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1399)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
at com.mycompany.busscan.pedigree.PedigreeScanFragment.setEcuFragmentAddress(PedigreeScanFragment.java:225)
at com.mycompany.busscan.pedigree.PedigreeScanFragment.showEcuDefaultDtc(PedigreeScanFragment.java:141)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment.restoreEcuSelection(VehicleBusScanFragment.java:514)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment.access$1300(VehicleBusScanFragment.java:57)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment$8.onPostExecute(VehicleBusScanFragment.java:808)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment$8.onPostExecute(VehicleBusScanFragment.java:775)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-08 10:45:40.787 5950-5950/com.mycompany.BusScan E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.BusScan, PID: 5950
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1399)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
at com.mycompany.busscan.pedigree.PedigreeScanFragment.setEcuFragmentAddress(PedigreeScanFragment.java:225)
at com.mycompany.busscan.pedigree.PedigreeScanFragment.showEcuDefaultDtc(PedigreeScanFragment.java:141)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment.restoreEcuSelection(VehicleBusScanFragment.java:514)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment.access$1300(VehicleBusScanFragment.java:57)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment$8.onPostExecute(VehicleBusScanFragment.java:808)
at com.mycompany.busscan.pedigree.VehicleBusScanFragment$8.onPostExecute(VehicleBusScanFragment.java:775)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
to...@gmail.com <to...@gmail.com> #44
Since recently (possibly after updating support library to: support-v4/23.0.0)
Fragment.class.getDeclaredField("mChildFragmentManager");
started throwing NoSuchFieldException.
A workaround is to search through getDeclaredFields() array.
Anyone noticed this?
Fragment.class.getDeclaredField("mChildFragmentManager");
started throwing NoSuchFieldException.
A workaround is to search through getDeclaredFields() array.
Anyone noticed this?
di...@gmail.com <di...@gmail.com> #45
Any chance of this being fixed?
th...@gmail.com <th...@gmail.com> #46
#32 did work for me..THANK YOU! already wasted 6 hours for this!
do...@gmail.com <do...@gmail.com> #47
I had the same exception as #35 and #32 fixed it.
My app has a MainActivity that loads a Fragment masterFragment into a FrameLayout, then that masterFragment loads a ViewPager and TabLayout with three more fragments from a getChildFragmentManager() instance. Clicking the back button goes to the MainActivity, and loading the masterFragment again crashes with #35's exception.
(With getSupportFragmentManager() there's no crash but the views are otherwise messed up, e.g. when you restore the view the paging is off until you swipe back to the beginning. It's proper to use child fragment manager from nested fragments. And whose convoluted design is that anyway?)
My code works fine with a FragmentPagerAdapter, but switching to FragmentStatePagerAdapter causes the crash, and BaseFragmentStatePagerAdapter from #35 totally fixes the issue.
Why is this marked "Closed" but also "Future Release"? Talk about a confusing bug report system. And we can't even see the internal comments if there are any. Maybe take a page out of Apple's book and put your Android project up on github?
I don't like Java as much as the next guy, but at least fix the bugs to make it less painful for all of us! People here have proposed several solutions that stop the exceptions--surely one of them is correct. I shouldn't have to be rummaging through 3 year old closed bug reports to download some random class to make my code work as advertised.
My app has a MainActivity that loads a Fragment masterFragment into a FrameLayout, then that masterFragment loads a ViewPager and TabLayout with three more fragments from a getChildFragmentManager() instance. Clicking the back button goes to the MainActivity, and loading the masterFragment again crashes with #35's exception.
(With getSupportFragmentManager() there's no crash but the views are otherwise messed up, e.g. when you restore the view the paging is off until you swipe back to the beginning. It's proper to use child fragment manager from nested fragments. And whose convoluted design is that anyway?)
My code works fine with a FragmentPagerAdapter, but switching to FragmentStatePagerAdapter causes the crash, and BaseFragmentStatePagerAdapter from #35 totally fixes the issue.
Why is this marked "Closed" but also "Future Release"? Talk about a confusing bug report system. And we can't even see the internal comments if there are any. Maybe take a page out of Apple's book and put your Android project up on github?
I don't like Java as much as the next guy, but at least fix the bugs to make it less painful for all of us! People here have proposed several solutions that stop the exceptions--surely one of them is correct. I shouldn't have to be rummaging through 3 year old closed bug reports to download some random class to make my code work as advertised.
ia...@gmail.com <ia...@gmail.com> #48
I've a workaround for NullPointerException when remove fragment in on DestoryView, Just put your code in onStop() not onDestoryView. It works fine!
@Override
public void onStop() {
super.onStop();
if (mMap != null) {
mfragmentManager.beginTransaction()
.remove(mfragmentManager.findFragmentById(R.id.location_map)).commit();
mMap = null;
}
}
@Override
public void onStop() {
super.onStop();
if (mMap != null) {
mfragmentManager.beginTransaction()
.remove(mfragmentManager.findFragmentById(R.id.location_map)).commit();
mMap = null;
}
}
am...@gmail.com <am...@gmail.com> #49
I am facing very big issue in marshmallow, when i am goding change permission state, and resume that app, it will force close by this error,
do you please help me to resolve it.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sa_projects, PID: 31201
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sa_projects/com.sa_projects.activity.NavigationDrawerActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.dmcapps.navigationfragment.manager.SingleStackNavigationManagerFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.dmcapps.navigationfragment.manager.SingleStackNavigationManagerFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:431)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:102)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
at com.sa_projects.activity.base.BaseNavigationDrawerActivity.onCreate(BaseNavigationDrawerActivity.java:80)
at com.sa_projects.activity.NavigationDrawerActivity.onCreate(NavigationDrawerActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.InstantiationException: java.lang.Class<com.dmcapps.navigationfragment.manager.SingleStackNavigationManagerFragment> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.support.v4.app.Fragment.instantiate(Fragment.java:420)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:102)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
at com.sa_projects.activity.base.BaseNavigationDrawerActivity.onCreate(BaseNavigationDrawerActivity.java:80)
at com.sa_projects.activity.NavigationDrawerActivity.onCreate(NavigationDrawerActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
do you please help me to resolve it.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sa_projects, PID: 31201
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sa_projects/com.sa_projects.activity.NavigationDrawerActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.dmcapps.navigationfragment.manager.SingleStackNavigationManagerFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.dmcapps.navigationfragment.manager.SingleStackNavigationManagerFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:431)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:102)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
at com.sa_projects.activity.base.BaseNavigationDrawerActivity.onCreate(BaseNavigationDrawerActivity.java:80)
at com.sa_projects.activity.NavigationDrawerActivity.onCreate(NavigationDrawerActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.InstantiationException: java.lang.Class<com.dmcapps.navigationfragment.manager.SingleStackNavigationManagerFragment> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.support.v4.app.Fragment.instantiate(Fragment.java:420)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:102)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
at com.sa_projects.activity.base.BaseNavigationDrawerActivity.onCreate(BaseNavigationDrawerActivity.java:80)
at com.sa_projects.activity.NavigationDrawerActivity.onCreate(NavigationDrawerActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
[Deleted User] <[Deleted User]> #50
This definitely does not belong here....
Create an empty constructor for your fragment. It even tells you so in the stacktrace?
Create an empty constructor for your fragment. It even tells you so in the stacktrace?
cl...@gmail.com <cl...@gmail.com> #51
Why is this issue closed? I can reproduce it (compile/target SDK version 22)
ha...@gmail.com <ha...@gmail.com> #52
This issue still persist .My target SDK version is 22 and I am using support lib version 23.0.3
[Deleted User] <[Deleted User]> #54
Above workarounds are not working.Is it any other work around for this issue.Is it released on support library 24.1.1? When it will released? Google devs please provide any work around for this issue.
lb...@gmail.com <lb...@gmail.com> #55
@54 Not sure if it can help, but here:
private static void runTillSucceededWithoutIllegalStateException(@NonNull final Activity activity, @NonNull final Handler handler, @NonNull final Runnable runnable) {
Runnable runnable2 = new Runnable() {
@Override
public void run() {
if (isActivityFinishingOrDestroyed(activity))
return;
try {
runnable.run();
} catch (IllegalStateException e) {
handler.post (this);
}
}
};
runnable2.run();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isActivityFinishingOrDestroyed(final Activity activity) {
if (activity == null || activity.isFinishing())
return true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
return activity.isDestroyed();
return false;
}
Call the runTillSucceededWithoutIllegalStateException methond, providing the fragment modification within the runnable
private static void runTillSucceededWithoutIllegalStateException(@NonNull final Activity activity, @NonNull final Handler handler, @NonNull final Runnable runnable) {
Runnable runnable2 = new Runnable() {
@Override
public void run() {
if (isActivityFinishingOrDestroyed(activity))
return;
try {
runnable.run();
} catch (IllegalStateException e) {
}
}
};
runnable2.run();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isActivityFinishingOrDestroyed(final Activity activity) {
if (activity == null || activity.isFinishing())
return true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
return activity.isDestroyed();
return false;
}
Call the runTillSucceededWithoutIllegalStateException methond, providing the fragment modification within the runnable
al...@android.com <al...@android.com> #56
There are too many unrelated bugs conflated on this issue. Please file a new issue so that we can triage and update accordingly.
ko...@gmail.com <ko...@gmail.com> #57
From stackoverflow by Marcus Forsell Stahre: https://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed post from 2013..
This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
If you look at the implementation of Fragment, you'll see that when moving to the detached state, it'll reset its internal state. However, it doesn't reset mChildFragmentManager (this is a bug in the current version of the support library). This causes it to not reattach the child fragment manager when the Fragment is reattached, causing the exception.
This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
If you look at the implementation of Fragment, you'll see that when moving to the detached state, it'll reset its internal state. However, it doesn't reset mChildFragmentManager (this is a bug in the current version of the support library). This causes it to not reattach the child fragment manager when the Fragment is reattached, causing the exception.
al...@google.com <al...@google.com>
an...@gmail.com <an...@gmail.com> #58
Even after applying workaround suggested by by Marcus Forsell we are still facing this exception
zb...@gmail.com <zb...@gmail.com> #59
Just addressing a crash that's trying to use the mChildFragmentManager and I've checked support-v4 Fragment source and looks like in performDetach() it releases already. Not exactly sure which version of the support library this source is but at 25.4.0, it's like this:
https://github.com/aosp-mirror/platform_frameworks_support/blob/2b11ffc59fb6f7305cade22217cc8b9b78be70f6/fragment/java/android/support/v4/app/Fragment.java#L2354
Any idea if this actually fixes this bug originally reported?
Any idea if this actually fixes this bug originally reported?
Description
This behavior has been observed in both the support v4 library and in the standard API (android.app.Fragment). The stack trace shown below belongs to API level 17 for the attached sample code.
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.self/edu.self.A}: java.lang.IllegalStateException: No activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: No activity
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1840)
at android.app.Fragment.performActivityCreated(Fragment.java:1709)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.Activity.performStart(Activity.java:5113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
... 11 more
Android 4.2.1 JOP40D on a Galaxy Nexus. The outputs of adb bugreport and adb logcat are included in the archive as bugreport.txt and log.txt respectively.