内存溢出OOM,求高手解救

时间:2022-07-30 20:56:59
错误提示的log如下
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snscity.member/com.snscity.member.WelcomeActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
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:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1946)
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
... 23 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:359)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3558)
at android.view.View.<init>(View.java:3488)
at android.view.ViewGroup.<init>(ViewGroup.java:464)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
... 26 more
android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1946)
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
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:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
... 23 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:359)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3558)
at android.view.View.<init>(View.java:3488)
at android.view.ViewGroup.<init>(ViewGroup.java:464)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
... 26 more





布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/welcome_bg"
    tools:context=".WelcomeActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:gravity="center"
        android:text="@string/activity_welcome_titles"
        android:textColor="@color/welcome_color"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/activity_welcome_tv_version"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="25dp"
        android:gravity="center"
        android:textColor="@color/version_color"
        android:textSize="18sp" />

</RelativeLayout>



RelativeLayout  中的background的图片在hdpi和xhdpi中都有。
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)  这句话对应的是activity中的  setContentView(R.layout.activity_welcome)这句


求大神搭救~

9 个解决方案

#1


问题太长,logcat后半部分放到 这里   
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1946)
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
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:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:359)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.&lt;init&gt;(View.java:3558)
at android.view.View.&lt;init&gt;(View.java:3488)
at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:464)
at android.widget.RelativeLayout.&lt;init&gt;(RelativeLayout.java:236)
... 26 more
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:359)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.&lt;init&gt;(View.java:3558)
at android.view.View.&lt;init&gt;(View.java:3488)
at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:464)
at android.widget.RelativeLayout.&lt;init&gt;(RelativeLayout.java:236)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1946)
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
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:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)

#2


android:background="@drawable/welcome_bg" 这个图片很大么? 去掉试试。

#3


welcome_bg 这个最好控制在几百K以内

#4


引用 2 楼 birdsaction 的回复:
android:background="@drawable/welcome_bg" 这个图片很大么? 去掉试试。


这张图片46.9KB,一点也不大

#5


引用 3 楼 birdsaction 的回复:
welcome_bg 这个最好控制在几百K以内
   这个bug也不是必现

#6


OutOfMemory并不一定是当前图片的问题,这就好比你有一个桶里面装了水,一开始你放一块很大的石头导致水马上要溢出了,这时候你再放一块很小的石头就导致水溢出了,你的这张图片很有可能就是这块小石头。
排除方法可以借助Android studio最新版的Memory Monitor功能监控你的APP内存情况,当然也可以dump一下然后利用Memory Analyzer排除了。

#7


bitmap不用了要及时手动 recycle

#8


引用 5 楼 X_i_a_o_H_a_i 的回复:
Quote: 引用 3 楼 birdsaction 的回复:

welcome_bg 这个最好控制在几百K以内
   这个bug也不是必现

图片很大有两个意思,一个是图片本身的大小,就是你说的46.9KB,另外就是图片的像素,也是就长度和宽度,这个很大程度上回决定占用内存大小,一般是图片的长像素数*宽像素*4,也就是说如果你的图片是1024*1024宽和高,那么占用内存就是4M。

#9


解决了吗?。

#1


问题太长,logcat后半部分放到 这里   
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1946)
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
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:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:359)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.&lt;init&gt;(View.java:3558)
at android.view.View.&lt;init&gt;(View.java:3488)
at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:464)
at android.widget.RelativeLayout.&lt;init&gt;(RelativeLayout.java:236)
... 26 more
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:359)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.&lt;init&gt;(View.java:3558)
at android.view.View.&lt;init&gt;(View.java:3488)
at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:464)
at android.widget.RelativeLayout.&lt;init&gt;(RelativeLayout.java:236)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1946)
at com.snscity.member.WelcomeActivity.onCreate(WelcomeActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
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:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)

#2


android:background="@drawable/welcome_bg" 这个图片很大么? 去掉试试。

#3


welcome_bg 这个最好控制在几百K以内

#4


引用 2 楼 birdsaction 的回复:
android:background="@drawable/welcome_bg" 这个图片很大么? 去掉试试。


这张图片46.9KB,一点也不大

#5


引用 3 楼 birdsaction 的回复:
welcome_bg 这个最好控制在几百K以内
   这个bug也不是必现

#6


OutOfMemory并不一定是当前图片的问题,这就好比你有一个桶里面装了水,一开始你放一块很大的石头导致水马上要溢出了,这时候你再放一块很小的石头就导致水溢出了,你的这张图片很有可能就是这块小石头。
排除方法可以借助Android studio最新版的Memory Monitor功能监控你的APP内存情况,当然也可以dump一下然后利用Memory Analyzer排除了。

#7


bitmap不用了要及时手动 recycle

#8


引用 5 楼 X_i_a_o_H_a_i 的回复:
Quote: 引用 3 楼 birdsaction 的回复:

welcome_bg 这个最好控制在几百K以内
   这个bug也不是必现

图片很大有两个意思,一个是图片本身的大小,就是你说的46.9KB,另外就是图片的像素,也是就长度和宽度,这个很大程度上回决定占用内存大小,一般是图片的长像素数*宽像素*4,也就是说如果你的图片是1024*1024宽和高,那么占用内存就是4M。

#9


解决了吗?。