带有图像的 ViewPager 导致应用程序崩溃

ViewPager with Image Causes the app crash

你好,我使用了 ViewPager 并制作了一个介绍滑块,除了幻灯片数量增加时程序崩溃和挂起外,一切正常,因为我使用了图像,加载图像会导致内存不足错误并使我不使用的应用程序崩溃不知道如何解决这个问题

这些是我的程序代码

private val introSliderAdapter = IntroSliderAdapter(
        listOf(
            IntroSliderModel(
                "test",
                "test2",
                R.drawable.slider1
            ),
            IntroSliderModel(
                "test",
                "test2",
                R.drawable.slider2
            )
        )
    )

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.intro_activity)
        introSliderViewPager.adapter = introSliderAdapter
}

这是我的适配器

class IntroSliderAdapter(private val introSlides: List<IntroSliderModel>) :
    RecyclerView.Adapter<IntroSliderAdapter.IntroSliderViewholder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IntroSliderViewholder {
        return IntroSliderViewholder(LayoutInflater.from(parent.context).inflate(R.layout.slider_item_container,parent,false))
    }

    override fun getItemCount(): Int {
        return introSlides.size
    }

    override fun onBindViewHolder(holder: IntroSliderViewholder, position: Int) {
        holder.bind(introSlides[position])
    }

    inner class IntroSliderViewholder(view: View) : RecyclerView.ViewHolder(view){
        private val textTitle = view.findViewById<MaterialTextView>(R.id.textTitle)
        private val textDescription = view.findViewById<MaterialTextView>(R.id.textDescription)
        private val imageIcon = view.findViewById<AppCompatImageView>(R.id.imageSlideIcon)

        fun bind(introSlides: IntroSliderModel) {
            textTitle.text = introSlides.title
            textDescription.text = introSlides.description
            imageIcon.setImageResource(introSlides.icon)
        }
    }
}

更新:

2020-02-28 17:23:23.027 16808-16808/ir.mahdi.circulars E/PushPole: Exception caught 
    java.lang.OutOfMemoryError: Failed to allocate a 256000012 byte allocation with 16777216 free bytes and 116MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
        at android.content.res.Resources.getDrawable(Resources.java:1640)
        at android.content.Context.getDrawable(Context.java:525)
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:455)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:132)
        at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
        at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:90)
        at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:98)
        at ir.mahdi.circulars.Adapter.IntroSliderAdapter$IntroSliderViewholder.bind(IntroSliderAdapter.kt:35)
        at ir.mahdi.circulars.Adapter.IntroSliderAdapter.onBindViewHolder(IntroSliderAdapter.kt:24)
        at ir.mahdi.circulars.Adapter.IntroSliderAdapter.onBindViewHolder(IntroSliderAdapter.kt:13)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1391)
        at androidx.recyclerview.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1116)
        at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:1838)
        at androidx.recyclerview.widget.RecyclerView.scrollByInternal(RecyclerView.java:1940)
        at androidx.recyclerview.widget.RecyclerView.onTouchEvent(RecyclerView.java:3391)
        at androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl.onTouchEvent(ViewPager2.java:991)
        at android.view.View.dispatchTouchEvent(View.java:10723)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2859)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2535)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
2020-02-28 17:23:23.027 16808-16808/ir.mahdi.circulars E/PushPole:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:559)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1870)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3236)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:521)
        at android.view.View.dispatchPointerEvent(View.java:10952)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5121)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4973)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4504)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4557)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4523)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4656)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4531)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4713)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4504)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4557)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4523)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4531)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4504)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7011)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6940)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6901)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7121)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7085)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7148)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
        at android.view.Choreographer.doCallbacks(Choreographer.java:702)
        at android.view.Choreographer.doFrame(Choreographer.java:632)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

你应该考虑使用一些图片加载库:Glide / Picasso / Fresco