IllegalStateException: Fragment Already Added when resuming app - java

Overview:
I have a fragment-based app with a single activity. On launch, I add my main fragment and all works well. When I tap on something, I call replace() to swap out to the new fragment and add the transaction to the back stack.
Problem:
When the app dies in the background (or most easily repro'd with 'Don't Keep Activities'), and I relaunch the app from the home screen, I get the exception below.
Scenario:
1. Open app
2. Navigate to a secondary fragment (this problem doesn't occur if you never leave the main fragment)
3. Background the app
4. Let the app die naturally (~24hr), or enable 'Don't Keep Activites' in the Developer Options
5. Open app from home screen
Expected:
App opens to main fragment (regardless of what fragment was displayed when backgrounded)
Actual:
Crash below
Other Requirements:
- Background app and immediately tap from home screen must return to previous fragment (assuming it wasn't destroyed in the background).
Stack Trace (note absolutely no calls from my app itself, so nowhere for me to debug):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rcg.socialstitch/com.rcg.socialstitch.ViewStitchActivity}: java.lang.IllegalStateException: Fragment already added: ViewStitchFragment{42800520 #1 id=0x7f040030}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalStateException: Fragment already added: ViewStitchFragment{42800520 #1 id=0x7f040030}
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1133)
at android.app.BackStackRecord.run(BackStackRecord.java:618)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(NativeStart.java)

I believe I have solved this. My .replace() calls all added to the back stack, but my initial .add() did not. When I ensured my initial fragment addition was no the back stack, this no longer seems to happen.

Related

Leaked IntentReceiver Exception using osmdroid

I have in my Android app a MapActivity, that shows map using osmdroid (Open Street Map for Android library).
When I'm in this MapActivity and I go to another activity, I get always an exception, and I cannot understand how to avoid it, because I think that it is related to something done inside the library.
As you can see in the following log, it happen after destroying of MapActivity
05-18 17:34:31.117 27073-27073/com.fpricoco.etip I/﹕ Classe 'MapActivity' ---- 'onDestroy()' ---- Stato: Distrutta
05-18 17:34:31.157 27073-27073/com.fpricoco.etip E/ActivityThread﹕ Activity com.fpricoco.etip.Activities.MapActivity has leaked IntentReceiver org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase$MyBroadcastReceiver#43d85f60 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.fpricoco.etip.Activities.MapActivity has leaked IntentReceiver org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase$MyBroadcastReceiver#43d85f60 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:792)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1151)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1138)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1132)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:365)
at org.osmdroid.tileprovider.util.SimpleRegisterReceiver.registerReceiver(SimpleRegisterReceiver.java:21)
at org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase.<init>(MapTileFileStorageProviderBase.java:34)
at org.osmdroid.tileprovider.modules.MapTileFileArchiveProvider.<init>(MapTileFileArchiveProvider.java:57)
at org.osmdroid.tileprovider.modules.MapTileFileArchiveProvider.<init>(MapTileFileArchiveProvider.java:76)
at org.osmdroid.tileprovider.MapTileProviderBasic.<init>(MapTileProviderBasic.java:63)
at org.osmdroid.tileprovider.MapTileProviderBasic.<init>(MapTileProviderBasic.java:41)
at org.osmdroid.tileprovider.MapTileProviderBasic.<init>(MapTileProviderBasic.java:34)
at org.osmdroid.views.overlay.MinimapOverlay.<init>(MinimapOverlay.java:114)
at com.fpricoco.etip.Activities.MapActivity.onCreate(MapActivity.java:303)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:137)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4791)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Searching on Google I found that I have to "unregisterReceiver(yourReceiver)", the point is that I don't have in my case any "yourReceiver" setted in my code, I think it is done inside library.
I also found this solution "https://groups.google.com/forum/#!topic/osmdroid/wDEvFpWfTIU" , but I don't understand how to detach
Finally I also tried in this way, but it didn't work:
// .. get custom tiles off the screen
myMapView.getOverlays().remove(mMinimapOverlay);
// .. release all custom tiles' bitmaps
myMapView.getTileProvider().clearTileCache();
// .. unregister intent receiver
myMapView.getTileProvider().detach();
The library I imported is:
compile 'org.osmdroid:osmdroid-android:5.1#aar'
inside my gradle.build file.
try this:
#Override
public void onDestroy() {
myMapView.onDetach();
myMapView.getTileProvider().clearTileCache();
}

java.lang.IllegalStateException: Fragment does not have a view

I have an activity which displays a DialogFragment, inside that dialog fragment I have a linearlayout which contains one button, and a fragmentLAyout where I want to display the info.
I´d have to display one list of elements, and if any of them are pressed I´ll have to display the details for them. I thought the best way to do that is with fragments, when trying to call
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.framePlaceHolder, listAccountFragment).commit();
after creating my listAccountFragment, I´m getting this error
10-01 10:53:34.004 12836-12836/com.bbva.compassBuzz.qa.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bbva.compassBuzz.qa.debug, PID: 12836
java.lang.IllegalStateException: Fragment does not have a view
at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1746)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:943)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.Fragment.performResume(Fragment.java:1836)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:993)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
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:1384)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1179)

Xamarin Android Theme.AppCompat exception

I'm writing an app in Xamarin for Android, and all is going well, but all of a sudden, i'm getting this "java.lang.illegalStateExeption error when i run the program.
I have done nothing to the code that might have caused this, and i've even gone back to copies of the program that i've backed up, where i know there was nothing wrong, and it's still throwing the exception.
The exception is thrown on the first line of the OnCreate method, here:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // error thrown here
and this is the error:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
At the top of the Main activity, i DO declare an AppCompat theme, like this:
[Activity(Label = "FrogPoint", Theme = "#style/Theme.AppCompat.Light", LaunchMode = LaunchMode.SingleTask, ScreenOrientation = ScreenOrientation.Portrait)] //master copy
public class MainActivity : ActionBarActivity, IBeaconConsumer
{
this is driving me nuts, particularly as it's just started happening for no apparent reason. Every time the app runs, the error is thrown.
I have tried both with, and without, the following in the Android Manifest:
<activity android:name="MainActivity" android:theme="#style/Theme.AppCompat" android:label="#string/app_name"></activity>
It makes no difference.
Can anybody please help?
Here is the stack trace:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:102)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at frogpoint.droid.MainActivity.n_onCreate(Native Method)
at frogpoint.droid.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
The problem was a corrupt Resource.Designer file. I deleted it from the project, then rebuilt the project and added the file back (it doesn't add it back to the project automatically). Then the problem was gone!

getResources returns null

We register a broadcast receiver to receive package installment or uninstallment event.
But some users report crash reports like this:
java.lang.RuntimeException: Unable to create application com.kc.security.MoSecurityApplication: java.lang.RuntimeException: getResources is null: dir - /data/app/com.cm.mg-1.apk, srcVal-1, srcVal-2
at android.app.LoadedApk.makeApplication(LoadedApk.java:495)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2249)
at android.app.ActivityThread.access$1600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: getResources is null: dir - /data/app/com.cm.mg-1.apk, srcVal-1, srcVal-2
at com.kc.security.b.ab.b(UpdateManager.java:69)
at com.kc.security.b.ab.a(UpdateManager.java:112)
at com.kc.security.MoSecurityApplication.onCreate(MoSecurityApplication.java:66)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
... 11 more
It is not easy to reappear this crash. We think a lot about this problem. We receive a
uninstall event ,then the Application context Class called it's onCreate method, we do
some initial works there , when we call getResouce(), it returns null . When look deep
inside the framework codes, we find that getReource locate resources file by sourceDir
String in ApplicationInfo, and the String is "/data/app/com.cm.mg-1.apk" > it's the original apk , and it is not exists anymore.
It maybe happens when user update our application. In some way "/data/app/com.cm.mg-1.apk" is allready deleted and replace by "/data/app/com.cm.mg-2.apk" any way, but the "sourceDir" String in ApplicationInfo is not update.
I want to know what android does when doing a updating.
Encountered a similar problem, check this out.
RuntimeException: Unable to instantiate application
I think this should answer this question, maybe because the Resources has been destroyed during this uninstall event.

How do I find this RuntimeException in Android (stack trace)?

I have this strack trace from the Developer Console on Google Play so I have no way to reproduce this exception cause I don't know what triggers it. The user also didn't leave any message, so...
java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5319)
at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:5271)
at android.widget.AbsListView$LayoutParams.<init>(AbsListView.java:6398)
at android.widget.AbsListView.generateLayoutParams(AbsListView.java:6035)
at android.widget.AbsListView.generateLayoutParams(AbsListView.java:96)
at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.android.internal.view.menu.ListMenuPresenter$MenuAdapter.getView(ListMenuPresenter.java:253)
at android.widget.AbsListView.obtainView(AbsListView.java:2212)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
at android.view.View.measure(View.java:12863)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2552)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
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:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
How do I pinpoint the problem?
This is Android's internal error, nothing in your code. You don't even see your app's code in stack trace, and your app works on many other devices.
I'm getting similar occasional crash reports, and also similar crashes on some Android versions (using ACRA). I've seen "You must supply a layout_height attribute" unexpected crash a few times.
If you use ACRA or similar, sometimes you just get reports for which your app is not responsible at all.
Message says - "You must supply a layout_height attribute."
So, double check that all your components have layout_height attribute.
I also had mention "menu" in this stack. It is generated by system. Double check that your all menus have valid items (at least not zero items) all the time.

Categories