java.lang.OutOfMemoryError:无法分配36000012字节分配16777216个空闲字节和21MB直到OOM

时间:2016-04-15 16:48:36

标签: java android android-studio out-of-memory

我使用listView创建了一个应用程序,listView包含使用数组的文本和图片。我有一个editText,用户可以使用搜索功能搜索列表。 listView加载正常但是当我点击editView搜索我的listView时,我得到outOfMemoryError。我不确定这是否与图像的大小有关。我知道之前已经问过这样的问题,但是他们都没有解决我的问题。

我的代码就像休耕一样

Insurance.java

public class Insurance extends AppCompatActivity {
    ListView list;
    ArrayAdapter<String> newadapter;
    EditText inputSearch;
    String[] itemname ={
            "AA",
            "Acorn",
            "Admiral",
            "AIG",
            "Allianz",
            "Auto Direct",
            "Aviva",
            "Carrot",


    };

    Integer[] imgid= {
            R.drawable.aa,
            R.drawable.acorn,
            R.drawable.admiral,
            R.drawable.aig,
            R.drawable.allianz,
            R.drawable.autodirect,
            R.drawable.aviva,
            R.drawable.carrot,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurance);

        CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
        list=(ListView)findViewById(R.id.list);
        newadapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.item, itemname);
        inputSearch = (EditText) findViewById(R.id.editText);
        list.setAdapter(newadapter);
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                Insurance.this.newadapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                String Slecteditem = itemname[+position];
                Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
                if (Slecteditem.toString() == "AA") {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:028 9032 2232"));
                    startActivity(intent);
                }
                if (Slecteditem.toString() == "Carrot") {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:028 9032 2265"));
                    startActivity(intent);
                }
            }
        });

Insurance.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/bg"
    tools:context="saveourcar.soc.Insurance">

    <EditText
        android:layout_width="400dp"
        android:layout_height="60dp"
        android:id="@+id/editText"
        android:layout_marginBottom="50dp"

/>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="107dp">
    </ListView>



</RelativeLayout>

myList.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="10"
    android:background="#26BFDA">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="2"

        >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:padding="5dp"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="6"

        android:orientation="vertical"

        >

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"

            />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView"
            />


    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"

        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="20dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/nextarrow"
            />
    </LinearLayout>
</LinearLayout>

我还在我的清单文件中包含了android:largeHeap="true"

logcat的

04-15 17:46:49.667 27237-27237/saveourcar.soc E/AndroidRuntime: FATAL EXCEPTION: main
Process: saveourcar.soc, PID: 27237
java.lang.OutOfMemoryError: Failed to allocate a 36000012 byte allocation with 16777216 free bytes and 21MB 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:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2453)
at android.content.res.Resources.loadDrawable(Resources.java:2360)
at android.content.res.Resources.getDrawable(Resources.java:768)
at android.content.Context.getDrawable(Context.java:402)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at saveourcar.soc.CustomListAdapter.getView(CustomListAdapter.java:39)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17442)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17442)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1767)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
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.ru

1 个答案:

答案 0 :(得分:2)

java.lang.OutOfMemoryError: Failed to allocate a 36000012 byte allocation with 16777216 free bytes and 21MB until OOM
    ...
    at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
    at saveourcar.soc.CustomListAdapter.getView(CustomListAdapter.java:39)

您的CustomListAdapterImageView中放置了一个可绘制的资源。当对密度进行解码和缩放时,该可绘制资源是36000012字节。这相当于3000像素×3000像素的图像。

太大。找出这个资源是什么并修复它:

  

我还包括android:largeHeap =&#34; true&#34;在我的清单文件中。

请撤消。