问题描述
当我尝试打开聊天类时,有时会正常运行,有时会在setContentView行中显示OutOfMemoryError.我不明白是什么原因引起的?这是我的xml代码:
When i try to open my chat class , sometimes it runs properly , sometimes it gives OutOfMemoryError at setContentView line.I can't understand what is the cause of problem?Here is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarmesajlasma"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/ColorPrimary"
android:elevation="1dp">
<RelativeLayout
android:id="@+id/layoutmesajlasma"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">
<ImageButton
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_centerVertical="true"
android:background="#00000000"
android:longClickable="false"
android:src="@mipmap/geribas" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginLeft="35dp"
android:background="#00000000"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="85dp"
android:text="Abdurrahman"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerVertical="true"
android:textColor="#ffffff"
android:textIsSelectable="false"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:layout_below="@+id/toolbarmesajlasma"
android:background="@mipmap/maybehi">
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#00000000"
android:dividerHeight="4dp"
android:layout_alignParentTop="true"
android:layout_above="@+id/linearLayout" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@mipmap/bisiler_yaz_altplan"
android:id="@+id/linearLayout">
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bisiler_yaz"
android:hint="Bir mesaj yaz..."
android:imeOptions="actionNone"
android:textSize="14dp"
android:textStyle="bold"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="5dp"
android:capitalize="sentences"
android:allowUndo="true"
android:width="295dp"
android:inputType="textCapSentences" />
<ImageButton
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="bottom"
android:background="#00000000"
android:src="@mipmap/gonder"
android:text="Gonder"
android:layout_marginBottom="10dp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivYatay"
android:src="@drawable/horizontalrope"
android:layout_marginTop="56dp"
android:layout_marginLeft="118dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivCapraz"
android:src="@drawable/diagonalrope"
android:layout_marginTop="50dp"
android:layout_marginLeft="92dp"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:id="@+id/imageView6"
android:background="@mipmap/gri"
android:layout_marginTop="45dp" />
</FrameLayout>
这是错误:
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3708)
at android.widget.ImageView.<init>(ImageView.java:127)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1022)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at droxoft.armin.com.shappy.Mesajlasma.onCreate(Mesajlasma.java:223)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
推荐答案
您的OOM错误与您在日志跟踪中看到的正在加载的图像有关.
Your OOM error is related to the Images you are loading as I am seeing in your log trace.
请参见此,以更高效地加载大型位图:
See this for loading large Bitmaps more efficiently:
一个400 KB的图像文件可以轻松占用5-10 MB的RAM.
A 400 KB image file can easily take up 5-10 MB of RAM.
要动态请求增加堆大小,请使用android:largeHeap="true"
在manifest.xml中.
For requesting to incraese heap size dynamically, Use android:largeHeap="true"
in the manifest.xml.
或/并且仅使用适当的功能进行解码...
or/and just use the proper function to decode...
这篇关于SetContentView行中的Android OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!