带有进度条的getView()中的OutOfMemoryError

时间:2014-10-10 18:51:34

标签: android android-listview out-of-memory android-progressbar

我有一个ListView,每个项目都包含ProgressBarImageButton。但在某些设备中,OutOfMemoryError出现在列表适配器的getView()实现中。这是full stacktrace

发生错误的行如下:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    } else {
        if (mDrawable == null)
            mDrawable = mContext.getResources().getDrawable(R.drawable.progress);

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(R.layout.test_list_item, parent, false); // The crash occurs here.
        holder = new ViewHolder(convertView);
        holder.progressBarEpisodes.setProgressDrawable(mDrawable);
    }


    // Show season name.
    refreshProgress(position, holder);

    ...

}


private void refreshProgress(int position, ViewHolder holder) {
    holder.progressBarEpisodes.setMax(Integer.parseInt(items.get(position).getTotal()));
    holder.progressBarEpisodes.setProgress(Integer.parseInt(items.get(position).getProgress()));
}

我的列表项布局xml如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_selector"
    android:paddingLeft="16dp"
    android:paddingRight="8dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:paddingRight="16dp">

        ....

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:progress="50"/>


    </LinearLayout>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        style="@style/EpisodeActionButton"
        android:focusable="false"
        android:background="@drawable/list_item_selector_no_alpha"
        android:progressDrawable="@drawable/progress"/>

</LinearLayout>

另外,我已经定制了这样的进度条样式:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <solid
                android:color="@color/gray_500"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid
                    android:color="@color/secondary_500" />
            </shape>
        </clip>
    </item>
</layer-list>

你能帮我解决这个崩溃问题吗?

1 个答案:

答案 0 :(得分:1)

我会看一下堆栈跟踪的这一部分:

引起:java.lang.OutOfMemoryError        在android.graphics.BitmapFactory.nativeDecodeAsset(BitmapFactory.java)        在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)        在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)        在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)

在我看来,每次你给视图充气时,它都试图获得你正在显示的这个位图的新版本。我的建议是以编程方式设置该位图,并反复重复使用相同的位图,以便您不会同时将多个版本的内容加载到内存中。

你加载了多少这些人?如果要加载多个不同的图像,请尝试使用以下方法缓存它们:

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

尝试有效地重用,缓存和释放资源。