Android - 具有三个列表视图的片段崩溃,内存不足错误

时间:2014-11-15 01:28:07

标签: android android-listview

我已经制作了这个简单的片段布局 -

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/listView_airplanes"
        android:layout_width="170dp"
        android:layout_height="wrap_content" >
    </ListView>

    <ListView
        android:id="@+id/listView_fruits"
        android:layout_width="170dp"
        android:layout_height="wrap_content" >
    </ListView>

    <ListView
        android:id="@+id/listView_animals"
        android:layout_width="170dp"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

现在,每个列表视图中的每一行都应显示位于资源文件夹中的图像。

所以每个列表项的布局都是这样的 -

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/@null" />

</LinearLayout>

现在,我为每个listview制作了一个布局,但它看起来都一样 - 只有一个imageview。

现在代码 -

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.listsviews,container, false);

 lvTops = (ListView) v.findViewById(R.id.listView_airplanes);
         lvBottoms = (ListView) v.findViewById(R.id.listView_fruits);
         lvMiddle = (ListView) v.findViewById(R.id.listView_animals);

dbHand = new DbHandler(getActivity());

Cursor topCursor = dbHand.queryByCat("airplanes);
Cursor bottomCursor = dbHand.queryByCat("fruits");
Cursor middleCursor = dbHand.queryByCat("animals");


            getActivity().startManagingCursor(topCursor);


            getActivity().startManagingCursor(bottomCursor);


            getActivity().startManagingCursor(middleCursor);

            topadp = new TopsAdptor(getActivity(), topCursor);
            lvTops.setAdapter(topadp);

            botadp = new BottomsAdaptor(getActivity(), bottomCursor);
            lvBottoms.setAdapter(botadp);

            middleCursor = new MiddleAdptor(getActivity(), topCursor);
            lvMiddle.setAdapter(topadp);



}

正如您所看到的,我为每个列表视图使用自定义适配器类。

所有适配器代码都相同 - 这是一个例子 -

    class BottomsAdaptor extends CursorAdapter {

    public BottomsAdaptor(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        String pic = cursor.getString(cursor
                .getColumnIndex(Constants.ITEM_PIC_URL));

        ImageView iv = (ImageView) view.findViewById(R.id.imageView_lv_bottom);




        try {
            InputStream ims;
            ims = getActivity().getAssets().open(pic);
            Drawable d = Drawable.createFromStream(ims, null);
            iv.setImageDrawable(d);

        } catch (IOException e) {


            e.printStackTrace();
        }


    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup parent) {
        // TODO Auto-generated method stub
        return getActivity().getLayoutInflater().inflate(R.layout.bottoms_list_item, parent,
                false);
    }

}

现在的问题是,在少量图像中(让每个列表视图为5)它确实可以正常工作,但是当我在每个列表视图中提高图像数量时,然后向下滚动一个应用程序崩溃的列表视图,并在日志中显示 -

java.lang.OutOfMemoryError

现在我也试过像这样使用asynctask -

    class Asyinc extends AsyncTask<Void, Void, Void>{
     Cursor topCursor;
     Cursor bottomCursor;
     Cursor middleCursor;

    @Override
    protected Void doInBackground(Void... params) {

         topCursor = dbHand.queryByCat("tops");
         bottomCursor = dbHand.queryByCat("bottoms");
         middleCursor;= dbHand.queryByCat("dress");


        getActivity().startManagingCursor(topCursor);

        getActivity().startManagingCursor(bottomCursor);
        getActivity().startManagingCursor(middleCursor);


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);


        topadp = new TopsAdptor(getActivity(), topCursor);
        lvTops.setAdapter(topadp);


        botadp = new BottomsAdaptor(getActivity(), bottomCursor);
        lvBottoms.setAdapter(botadp);


        middleatp = new DressedAdptor(getActivity(), middleCursor);
        lvMiddle.setAdapter(middleatp);




    }

}

但是在尝试滚动列表视图时,应用程序仍然崩溃。

任何想法我怎么能解决这个问题? 感谢您提供任何帮助

当我使用asynctask -

时,这里出现了logcat错误
    11-15 00:54:15.635: E/AndroidRuntime(4050): FATAL EXCEPTION: main
11-15 00:54:15.635: E/AndroidRuntime(4050): java.lang.OutOfMemoryError
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:741)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at com.example.app1.MyFrag$BottomsAdaptor.bindView(MywordFrag.java:262)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.AbsListView.obtainView(AbsListView.java:2267)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.ListView.makeAndAddView(ListView.java:1769)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.ListView.fillDown(ListView.java:672)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.ListView.fillGap(ListView.java:636)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5036)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3193)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.AbsListView.startScrollIfNeeded(AbsListView.java:3138)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3463)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.View.dispatchTouchEvent(View.java:7127)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.View.dispatchPointerEvent(View.java:7307)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.os.MessageQueue.nativePollOnce(Native Method)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.os.MessageQueue.next(MessageQueue.java:125)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.os.Looper.loop(Looper.java:124)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at java.lang.reflect.Method.invoke(Method.java:511)
11-15 00:54:15.635: E/AndroidRuntime(4050):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:

1 个答案:

答案 0 :(得分:1)

图像尺寸太大或其他东西。你可以尝试添加

android:largeHeap="true"

在清单文件中的应用程序标记上。看看是否能解决问题。虽然不能保证解决它。如上所述:http://developer.android.com/guide/topics/manifest/application-element.html

但它确实修复了我的应用中的内存不足错误。看看这对你有帮助吗?