我有一个android应用,尝试绘制太大时崩溃了。我已经查看了在stackoverflow上发布的类似问题的答案,但是没有一种解决方案适合我。所有图像都在各自的文件夹中(请参阅附件图像),所以现在我对导致崩溃的原因不知所措android -  Canvas :试图绘制太大的位图(151099200bytes)-LMLPHP

    2019-11-27 11:22:49.608 10506-10506/com.example.android.tourguide E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.tourguide, PID: 10506
    java.lang.RuntimeException: Canvas: trying to draw too large(151099200bytes) bitmap.
        at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
        at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
        at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)
        at android.widget.ImageView.onDraw(ImageView.java:1286)
        at android.view.View.draw(View.java:17071)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.widget.ListView.drawChild(ListView.java:3506)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.widget.AbsListView.dispatchDraw(AbsListView.java:2635)
        at android.widget.ListView.dispatchDraw(ListView.java:3501)
        at android.view.View.draw(View.java:17074)
        at android.widget.AbsListView.draw(AbsListView.java:4298)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.draw(View.java:17074)
        at androidx.viewpager.widget.ViewPager.draw(ViewPager.java:2426)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.updateDisplayListIfDirty(View.java:16048)
        at android.view.View.draw(View.java:16837)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
        at android.view.View.draw(View.java:17074)
        at com.android.internal.policy.DecorView.draw(DecorView.java:751)
        at android.view.View.updateDisplayListIfDirty(View.java:16053)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
2019-11-27 11:22:49.608 10506-10506/com.example.android.tourguide E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)


我在这里使用ListView:

    public class AttractionsAdapter  extends ArrayAdapter<Attractions> {

    public AttractionsAdapter(Context context, ArrayList<Attractions> attractions) {
        super(context, 0, attractions);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if an existing view is being reused, otherwise inflate the view
        View listView = convertView;
        if (listView == null) {
            listView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item_layout, parent, false);
        }
    Attractions currentAttraction = getItem(position);

    TextView attractionTextView = listView.findViewById(R.id.attraction_name_view);

        attractionTextView.setText(currentAttraction.getName());


    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView imageView = (listView.findViewById(R.id.image));

        if (currentAttraction.hasImage()) {

        imageView.setImageResource(currentAttraction.getImageId());
        imageView.setVisibility(View.VISIBLE);
    } else {

        imageView.setVisibility(View.GONE);
    }

        return listView;
}

}


和我的片段:

public class ClubsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstances) {
        View rootView = inflater.inflate(R.layout.attraction_layout, container, false);

        ArrayList<Attractions> attractions = new ArrayList<>();
        attractions.add(new Attractions(R.string.club_one, R.drawable.shakers));
        attractions.add(new Attractions(R.string.club_two,R.drawable.jojo));
        attractions.add(new Attractions(R.string.club_three, R.drawable.maia));
        attractions.add(new Attractions(R.string.club_four, R.drawable.great));


        AttractionsAdapter adapter = new AttractionsAdapter(getActivity(), attractions);

        ListView listView = rootView.findViewById(R.id.list);

        listView.setAdapter(adapter);
        return rootView;
    }

}


和我的布局:

<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="wrap_content"
>
<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/image"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/attraction_name_view"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_weight="1"
        android:gravity="top"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        tools:text="one" />

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp" />
</RelativeLayout>

最佳答案

我通过删除所有我使用的超过2mb的图像解决了该问题。现在,该应用程序不再崩溃。
感谢@Bracadabra和@Vladyslav的评论。他们帮助我找出问题所在以及解决方法

08-04 00:43