当打开更多对话框导致内存错误时

时间:2019-04-27 15:34:54

标签: android memory android-dialogfragment

我正在RecyclerView中列出商品。如果单击每个项目,则会通过DialogFragment / AppCompatDialogFragment弹出详细信息。大约30次展示后,我遇到了内存错误,应用程序崩溃了。

我用dismiss()关闭对话框。我还使用了android:largeHeap="true",它只会增加印象数。最后,应用再次崩溃。

java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available

我该怎么办?您有什么建议?

fragment_item.class

public class FragmentSingle extends DialogFragment {

    private Item item;

    public FragmentSingle() {
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialog);
    }

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

        FragmentItemBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(getContext()), R.layout.fragment_item, null, false
        );

        binding.setItem(item);

        return binding.getRoot();
    }

    @Override
    public void onStop() {
        super.onStop();
        dismiss();
    }
}

fragment_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="item"
            type="...Item" />

    </data>


    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            loadImage="@{item.image}"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:padding="16dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    </android.support.constraint.ConstraintLayout>


</layout>
    @BindingAdapter("loadImage")
    public static void loadImage(View view, String url) {
        ImageView imageView = (ImageView) view;
        Picasso.get()
                .load(url)
                .fit()
                .centerCrop()
                .into(imageView);

    }

0 个答案:

没有答案