简体   繁体   English

如何处理此类OutOfMemoryErrors

[英]How to deal with such OutOfMemoryErrors

I'm testing my android App in some devices, and I've noticed a weird behavior in a certain mobile. 我正在某些设备上测试我的android应用程序,并且注意到某些移动设备上的行为异常。 When I open an Activity (which shows a bitmap) the first time it crashes generating this stack trace: 当我第一次打开一个活动(显示一个位图)时,它崩溃时生成了此堆栈跟踪:

   java.lang.RuntimeException: Unable to start activity ComponentInfo{example.perifereia_hpeirou/com.epirus.MonumentProjection}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:133)
at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:261)
at com.epirus.MonumentProjection.onCreate(MonumentProjection.java:77)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
... 24 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
at android.content.res.Resources.loadDrawable(Resources.java:2988)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3563)
at android.view.View.<init>(View.java:3492)
at android.view.ViewGroup.<init>(ViewGroup.java:469)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
... 27 more

However, if I try to open the activity again this doesn't happen.Also this doesn't occur in any other devices I've tested. 但是,如果我尝试再次打开活动,则不会发生这种情况,而且在我测试过的任何其他设备中也不会发生这种情况。 I guess the mobile doesn't have enough ram atm and this exception helps it clean memory but this is just my guess.What can I do in order to overcome such a problem? 我猜手机没有足够的ram atm,这种异常有助于清理内存,但这只是我的猜测。为了解决这个问题,我该怎么办?

some ideas you can try: 您可以尝试一些想法:

a) Try with a small image to see if it is due to the size of the bitmap. a)尝试使用小图像,看看是否是由于位图的大小所致。

b) The first cause you have states: b)您遇到的第一个原因是:

Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>

I would also check if the class you are using here is supported by that particular device. 我还要检查您在此使用的类是否受该特定设备的支持。

For example I remember an application I developed crashing with devices that weren't google certified because I assumed all devices would have google maps and this is not the case. 例如,我记得我开发的一个应用程序在未通过Google认证的设备上崩溃了,因为我认为所有设备都将安装Google Maps,但事实并非如此。 I had to add a runtime check before instanciating the object. 我必须在实例化对象之前添加运行时检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM