In my application, I am facing a weird issue. And this happens only on Android 6. It doesn't crash on version higher than 6.0
I cannot add code because I have no idea where exactly is the crash.
Is there any way to find out what can be an issue. All I have is a full stack trace.
I think this due change in behaviour of ListView in Android 6, but cannot exactly sure about it.
Fatal Exception: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.ListView.dispatchDraw(ListView.java:3327)
at android.view.View.draw(View.java:16276)
at android.widget.AbsListView.draw(AbsListView.java:4316)
at android.view.View.updateDisplayListIfDirty(View.java:15269)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3599)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3579)
at android.view.View.updateDisplayListIfDirty(View.java:15229)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3599)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3579)
at android.view.View.updateDisplayListIfDirty(View.java:15229)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3599)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3579)
at android.view.View.updateDisplayListIfDirty(View.java:15229)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3599)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3579)
at android.view.View.updateDisplayListIfDirty(View.java:15229)
at android.view.View.draw(View.java:16043)
at android.view.ViewGroup.drawChild(ViewGroup.java:3615)
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1379)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3405)
at android.view.View.draw(View.java:16276)
at android.view.View.updateDisplayListIfDirty(View.java:15269)
at android.view.View.draw(View.java:16043)
at android.view.ViewGroup.drawChild(ViewGroup.java:3615)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3405)
at android.view.View.updateDisplayListIfDirty(View.java:15264)
at android.view.View.draw(View.java:16043)
at android.view.ViewGroup.drawChild(ViewGroup.java:3615)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3405)
at android.view.View.updateDisplayListIfDirty(View.java:15264)
at android.view.View.draw(View.java:16043)
at android.view.ViewGroup.drawChild(ViewGroup.java:3615)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3405)
at android.view.View.updateDisplayListIfDirty(View.java:15264)
at android.view.View.draw(View.java:16043)
at android.view.ViewGroup.drawChild(ViewGroup.java:3615)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3405)
at android.view.View.updateDisplayListIfDirty(View.java:15264)
at android.view.View.draw(View.java:16043)
at android.view.ViewGroup.drawChild(ViewGroup.java:3615)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3405)
at android.view.View.draw(View.java:16276)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2740)
at android.view.View.updateDisplayListIfDirty(View.java:15269)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:295)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:301)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:336)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2760)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2564)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2164)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1174)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6241)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
I found a scenario which solved my issue.
I had a list which had its own data.
One option was to refresh list. So here, cleared ArrayList >> get new data from API (2-3 secs delay) >> added to ArrayList >> Notify dataset changed.
The problem was I didn't notify dataset immediately after clearing the ArrayList. (Because it would flicker)
Solution: I managed to clear data, add new data and notify at the same time (After API call).
Still wondering why did it only crashed on Android 6.0
Related
I have a Xamarin.Android application who contains a lot of button in her main game activity (25+).
When I press on a button, I randomly get a crash on my application. I have the following logs for the crash :
Xamarin caused by: java.lang.OutOfMemoryError: Failed to allocate a 44236812 byte allocation with 8344256 free bytes and 7MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)
at android.content.res.Resources.loadDrawable(Resources.java:4085)
at android.content.res.Resources.getDrawable(Resources.java:2005)
at android.content.res.Resources.getDrawable(Resources.java:1987)
at android.content.Context.getDrawable(Context.java:464)
at android.view.View.setBackgroundResource(View.java:18532)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I do not know what really happends and how to fix it. An interested fact is that when I install the app, I did not face any issue the first time I use it. If I close the app and restart it, I get random crash on a button click event. The button action is to set a différent background image on a ImageView.
You can find a snapshot of my method who change the background :
private void SetErrorImage(ImageView component, int errorNumber)
{
switch (errorNumber)
{
case 0:
component.SetBackgroundResource(Resource.Drawable.arbre_00);
break;
case 1:
component.SetBackgroundResource(Resource.Drawable.arbre_01);
break;
case 2:
component.SetBackgroundResource(Resource.Drawable.arbre_02);
break;
case 3:
component.SetBackgroundResource(Resource.Drawable.arbre_03);
break;
case 4:
component.SetBackgroundResource(Resource.Drawable.arbre_04);
break;
case 5:
component.SetBackgroundResource(Resource.Drawable.arbre_05);
break;
case 6:
component.SetBackgroundResource(Resource.Drawable.arbre_06);
break;
case 7:
component.SetBackgroundResource(Resource.Drawable.arbre_07);
break;
}
}
Following this link, is it possible to use something like this Java method in Xamarin ?
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
Not the reason where your app looses the memory but a way to make OutOfMemoryError less likely.
I see that android wants to allocate "44236812" bytes (44 Megabytes) for an uncompressed image that may come from a 2048*1536 pixel 1.6MB jpg file.
Have you tried to reduce your resource images (less pixels or less bits per pixel)? Is your device capable to display this high resolution?
As you kinda thought, you need to free the images properly.
Even just changing the orientation several times, will just allocate more and more memory, if you dont free it properly.
Basically just
imageView.SetImageDrawable(null)
should do the trick.
I get error when I use StaticLayout for create pagination of large text (~6mb).
This is my log:
java.lang.OutOfMemoryError
at android.text.MeasuredText.addStyleRun(MeasuredText.java:193)
at android.text.MeasuredText.addStyleRun(MeasuredText.java:269)
at android.text.StaticLayout.generate(StaticLayout.java:291)
at android.text.StaticLayout.<init>(StaticLayout.java:143)
at android.text.StaticLayout.<init>(StaticLayout.java:93)
at android.text.StaticLayout.<init>(StaticLayout.java:71)
at android.text.StaticLayout.<init>(StaticLayout.java:51)
at com.ex.test.pagenation.Pagination.layout(Pagination.java:38)
at com.ex.test.pagenation.Pagination.<init>(Pagination.java:34)
at com.ex.test.pagenation.MainActivity$1.onGlobalLayout(MainActivity.java:51)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2006)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Is there any suggestion for fix this error? Or Is there any alternative solution?
You should rewrite or extend Pagination class.
You should not put all the text into StaticLayout at once.
Instead, you should split text into parts. Parts should not be too lage.
I guess 10k-100k will be ok.
take first part, split it into pages, count pages, multiply it by amount of parts - that will be approximate quantity of pages in you book.
When a user want to see the past page of a first part, you should start second part just where the last page of first part begins. And so on.
Do not load all the text into StaticLayout - it is too much.
The relevant code:
runOnUiThread(new Runnable() {
#Override
public void run() {
ImageButton btn = (ImageButton) findViewById(R.id.info_image);
btn.setImageResource(currentID[currentIDPos]);
}
});
It's saying that the run method is making a 400-some megabyte allocation. My jpgs are no more than a megabyte in size. The run() method is called on a timer every 3-4 seconds or so and on click, but that shouldn't matter because from what I can tell using printlns the run() is only called once before the crash.
Edit: Here's the exception. Had to run it again to get this, it's a different amount of memory this time but still very large.
FATAL EXCEPTION: main
Process: com.grey.handsaver, PID: 32531
java.lang.OutOfMemoryError: Failed to allocate a 215151564 byte allocation with 33554400 free bytes and 100MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2580)
at android.content.res.Resources.loadDrawable(Resources.java:2487)
at android.content.res.Resources.getDrawable(Resources.java:814)
at android.content.res.XResources.getDrawable(XResources.java:572)
at android.content.Context.getDrawable(Context.java:403)
at android.widget.ImageView.resolveUri(ImageView.java:747)
at android.widget.ImageView.setImageResource(ImageView.java:398)
at com.grey.handsaver.ExerciseInfoActivity$3.run(ExerciseInfoActivity.java:128)
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:5430)
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:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
Try to use picasso library for loading large image.it's having inbuilt cache functionality.
Step 1:
First write follow code in gradle.build and sync gradle.
compile 'com.squareup.picasso:picasso:2.5.2'
Step 2:
Picasso.with(context).load("YOUR IMAGE PATH").into(btn);
and write above code where we load the image.in this code you must need to provide image path and make sure image path is valid.if you really want to learn something more about this Library than please Visit this.
I hope your are clear with my Idea.
Best of Luck
Most likely the issue is that you are not clearing up the previously allocated space. Are you sure that the fragment/activity that loads these pictures is also removed from backstack? Or maybe the images themselves are not being garbage collected?
Re-do the same steps that are causing the OutofMemory Exception but make sure to switch to Memory Monitor Tab in Android Studio and check where the Memory is increasing drastically but not being freed when it should be. This should give you a head start to see where the memory leak might be.
Also possible that there is no leak, rather your pictures themselves are huge (although doesnt seems to be). In this case you should do lazy loading, cache implementation etc. or just use some third party library like Picasso which handle this for you
Alright, I figured it out. I'm using an lg g4, which uses xxxhdpi, and my images were in the drawable folder, not the drawable-xxxhdpi folder. This solved my problem without using picasso or the other methods.
My application works great while using the emulator, but it crashes while working on real device. I code it for Android 2.2. Real device is 2.3.6.
Problem is here:
When I use something like that:
tvPlayerOneScores.setText("-" + numberFormat.format(level));
it works great both on emulator and real device.
But when I try this:
tvPlayerOneScores.setText("-" + numberFormat.format(level * 0.5f));
it works on emulator, but crashes on real device.
Stack Trace:
java.lang.NumberFormatException
at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
at java.lang.Float.parseFloat(Float.java:323)
at java.lang.Float.valueOf(Float.java:362)
at com.amrotek.truthordare.GameActivity$4.onClick(GameActivity.java:287)
at android.view.View.performClick(View.java:2552)
at android.view.View$PerformClick.run(View.java:9229)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
EDIT:
I multiplied all values by 10, so I only work with integer numbers when I do arithmetic operations. At the end I just divide end value by 10. It works :)
you should have included faulty line in the question not in the comments:
playerOnePoints = Float.valueOf(numberFormat.format(playerOnePoints - level * 0.5f));
since it's not enough to make a conclusion, here's a short list of things for you to check:
why do you need to format a number with numberFormat just to convert it back with valueOf?
what's the numberFormat, does it really produce valid float strings?
is playerOnePoints float or Float? should not you use parseFloat() instead of valueOf()?
Finally, if you still are unable to solve your problem, I'd suggest to replace the aforementioned line with simple:
playerOnePoints = playerOnePoints - level * 0.5f;
That should do the trick =)
ps. also you migth want to get acquainted with Math.floor()/ceil(), if I understood your intentions with parseFloat()/numberFormat() correctly.
I can't seem to get the camera to work on ICS. My original code worked fine in Froyo but now I'm getting this error in ICS:
02-27 11:54:46.781: E/CameraHAL(118): Sending shutter callback
02-27 11:54:46.820: E/CameraHAL(118): Empty Frame Queue
02-27 11:54:46.820: E/CameraHAL(118): Frame returned when ref count is already zero!!
02-27 11:54:46.875: E/CameraHAL(118): Adapter state switch PREVIEW_ACTIVE Invalid Op! event = 0x5
I was wondering if anyone knew what this meant or can point me to a working camera demo that works on ICS and I'll try to figure it out myself. I'm not pasting any code because I'm not even sure where it's happening. Thanks.
It seems like this was caused by stopPreview() for me. Removing it fixed it... but I don't know when I'm supposed to stop the preview now or if it's even necessary.