Listview中的内存不足错误

时间:2014-04-12 19:07:32

标签: android android-listview

我在1周后遇到此错误。一旦我将空listview放入linearlayout,我就会出现内存不足错误。 xml的结构是:

TabHost - >的ListView

请帮我找出让我收到错误的原因。

编辑:tabhost在没有ListView的情况下工作。我意识到了;即使我只使用ListView创建一个完整的新布局,它也失败了。

这是我的代码:

public class FriendsAndGroups extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.friendsandgroups);
        setUpLayout();

    }

    private void setUpLayout() {
        final TabHost tabHost = (TabHost) findViewById(R.id.tabHostFriendsAndGroups);
        tabHost.setup();

        final TabWidget tabWidget = tabHost.getTabWidget();
        final FrameLayout tabContent = tabHost.getTabContentView();

        // Get the original tab textviews and remove them from the viewgroup.
        TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
        for (int index = 0; index < tabWidget.getTabCount(); index++) {
            originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
        }
        tabWidget.removeAllViews();

        for (int index = 0; index < originalTextViews.length; index++) {
            final TextView tabWidgetTextView = originalTextViews[index];
            final View tabContentView = tabContent.getChildAt(index);
            final TabHost.TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
            tabSpec.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return tabContentView;
                }
            });
            if (tabWidgetTextView.getBackground() == null) {
                tabSpec.setIndicator(tabWidgetTextView.getText());
            } else {
                tabSpec.setIndicator(tabWidgetTextView.getText(), tabWidgetTextView.getBackground());
            }
            tabHost.addTab(tabSpec);
        }

        final TabWidget widget = tabHost.getTabWidget();
        final int tabChildrenCount = widget.getChildCount();
        LinearLayout.LayoutParams currentLayout;
        for (int i = 0; i < tabChildrenCount; i++) {
            currentLayout = (LinearLayout.LayoutParams) widget.getChildAt(i).getLayoutParams();
            if (i == 0) {
                currentLayout.setMargins(40, 0, 20, 0);
            } else {
                currentLayout.setMargins(20, 0, 40, 0);
            }
        }
        widget.requestLayout();

        originalTextViews = new TextView[tabWidget.getTabCount()];
        for (int index = 0; index < tabWidget.getTabCount(); index++) {
            originalTextViews[index] = (TextView) ((LinearLayout) tabWidget
                    .getChildTabViewAt(index)).getChildAt(1);
        }
        originalTextViews[0].setBackgroundResource(R.drawable.tableft);
        originalTextViews[1].setBackgroundResource(R.drawable.tabright);
        originalTextViews[0].setTextColor(Color.rgb(118, 119, 90));
        originalTextViews[1].setTextColor(Color.rgb(227, 228, 206));
        originalTextViews[0].setGravity(Gravity.CENTER);
        originalTextViews[1].setGravity(Gravity.CENTER);
        originalTextViews[0].setTypeface(null, Typeface.BOLD);
        originalTextViews[1].setTypeface(null, Typeface.BOLD);

        final LinearLayout tabFriends = (LinearLayout) findViewById(R.id.tabFriends);
        // LinearLayout tabGroups = (LinearLayout)findViewById(R.id.tabGroups);

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if (tabId.compareTo("0") == 0) {

                    final ListView listView = getListView();
                    final MyAdapter adapter = new MyAdapter(FriendsAndGroups.this,
                            R.layout.friendsandgroupsrow, new String[] {
                                    "singlesamplefortry"
                            });
                    listView.setAdapter(adapter);
                    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                    adapter.notifyDataSetChanged();

                }
            }
        });

        tabHost.setCurrentTab(1);
        tabHost.setCurrentTab(0);

    }

    public class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            final LayoutInflater inflater = getLayoutInflater();
            final View row = inflater.inflate(R.layout.friendsandgroupsrow, parent, false);
            return row;
        }
    }

}

这是我的主要xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/friendsAndGroupsBackButton"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:scaleType="fitXY"
            android:src="@drawable/friendsandgroups" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#cdceb9"
        android:orientation="vertical"
        android:paddingTop="10dp" >

        <TabHost
            android:id="@+id/tabHostFriendsAndGroups"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@android:color/transparent"
                android:orientation="vertical" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:showDividers="none" >

                    <TextView
                        android:id="@+id/textViewFriends"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:autoText="false"
                        android:background="@drawable/tableft"
                        android:clickable="true"
                        android:gravity="center"
                        android:tag="0"
                        android:text="Friends"
                        android:textColor="#76775a"
                        android:textSize="24dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textViewGrups"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:autoText="false"
                        android:background="@drawable/tabright"
                        android:clickable="true"
                        android:gravity="center"
                        android:tag="1"
                        android:text="Groups"
                        android:textColor="#e3e4ce"
                        android:textSize="24dp"
                        android:textStyle="bold" />
                </TabWidget>

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="#e3e4ce" >

                    <LinearLayout
                        android:id="@+id/tabFriends"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#e3e4ce"
                        android:orientation="vertical" >

                        <ListView
                            android:id="@android:id/list"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:headerDividersEnabled="true" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tabGroups"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#e3e4ce"
                        android:orientation="vertical" >
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    </LinearLayout>

</LinearLayout>

,这是listview项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="vertical"
    android:paddingTop="5dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/profilePicture"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="4"
            android:src="@drawable/profilepicturesmall" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Text"
                    android:textColor="#76775a"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Text"
                    android:textColor="#76775a" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Text"
                    android:textColor="#76775a" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="4"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageViewLine"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingTop="5dp" />
    </LinearLayout>

</LinearLayout>

最后错误是:

04-12 22:04:27.676: ERROR/AndroidRuntime(16878): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.CashFinance/com.CashFinance.FriendsAndGroups}: android.view.InflateException: Binary XML file line #69: Error inflating class android.widget.ListView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
        at android.app.ActivityThread.access$700(ActivityThread.java:168)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        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:1209)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.view.InflateException: Binary XML file line #69: Error inflating class android.widget.ListView
        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.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
        at android.app.Activity.setContentView(Activity.java:1956)
        at com.CashFinance.FriendsAndGroups.onCreate(FriendsAndGroups.java:30)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
        ... 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)
        ... 29 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.Resources.getDrawable(Resources.java:1558)
        at android.widget.EdgeEffect.<init>(EdgeEffect.java:148)
        at android.widget.AbsListView.setOverScrollMode(AbsListView.java:1223)
        at android.view.View.<init>(View.java:3454)
        at android.view.View.<init>(View.java:3514)
        at android.view.ViewGroup.<init>(ViewGroup.java:475)
        at android.widget.AdapterView.<init>(AdapterView.java:236)
        at android.widget.AbsListView.<init>(AbsListView.java:1108)
        at android.widget.ListView.<init>(ListView.java:151)
        at android.widget.ListView.<init>(ListView.java:147)
        ... 32 more

1 个答案:

答案 0 :(得分:0)

查看堆栈跟踪,看起来android在尝试加载位图时内存不足:

Caused by: java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    ....
    at android.content.res.Resources.loadDrawable(Resources.java:2988)

尝试制作"@drawable/profilepicturesmall"指向较小的图像。