Viewpager 不能为空 (Viewpager2)

Viewpager must not be null (Viewpager2)

我正在尝试在按下辅助 activity 上的按钮时实现 viewpager 为此我创建了一个 PageAdaper class 它扩展了 Recycler view.Adapter 。我得到的 viewpager 不能是运行时出现 null 错误。

PageAdaper.kt

open class PageAdapter(context: Context, arrayList: ArrayList<Uri>) : RecyclerView.Adapter<PageAdapter.MyViewHolder>() {
    private var mContext: Context? =null
    private var galleryUri= arrayListOf<Uri>()
    private var layoutInflater: LayoutInflater? =null
    init {
        this.mContext=context
        this.galleryUri=arrayList
        this.layoutInflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }
    private fun onBindViewHolderr(holder: MyViewHolder, position: Int) {
        holder.iView.setImageURI(galleryUri[position])
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view=LayoutInflater.from(mContext).inflate(R.layout.image_container,parent,false)
        return MyViewHolder(view)
    }
    override fun getItemCount(): Int {
        return galleryUri.size
    }
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iView: ImageView
        init {
            super.itemView
            iView=itemView.findViewById(R.id.imageContainer)
        }
    }
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        onBindViewHolderr(holder, position)
    }
}

Image_container.xml


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageContainer"
        android:contentDescription="@string/gallery" />
</LinearLayout>

Activity_gallery.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/galleryBack"
        android:layout_width="59dp"
        android:layout_height="56dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/back_button"
        android:src="@drawable/ic_baseline_arrow_back_24"
        style="@style/Widget.AppCompat.Button.Colored"
        app:layout_constraintHorizontal_bias="0.05"
        app:layout_constraintVertical_bias="0.05"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:background="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.viewpager2.widget.ViewPager2>

</androidx.constraintlayout.widget.ConstraintLayout>

点击来自 activity_main

的按钮时在 viewpager 上创建
gallery.setOnClickListener {    // gallery button reference
            if (lastImageUri!=null){
                val intent= Intent(this,GalleryActivity::class.java) 
                startActivity(intent)    // inflates activity_gallery.xml
                val iAdapter=PageAdapter(this,galleryUri) // creating object with params
                viewPager.adapter=iAdapter    // here comes the error
            }
        }

更新

GalleryActivity.kt

它只包含 setContentview 到 activity_gallery.xml 我需要添加什么吗?

class GalleryActivity : AppCompatActivity() {
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gallery)
        overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right)
        galleryBack.setOnClickListener {
            finish()
            overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right)
        }
    }
}

请向我解释一下,因为我是 android 的新手。谢谢!

所以,startActivity会去GalleryActivity class。您需要将之后的行放入 GalleryActivity.

好的,现在您已经添加了 GalleryActivity.kt。您需要进行 2 处更改。首先,在你的 activity_main:

gallery.setOnClickListener {    // gallery button reference
   if (lastImageUri!=null){
      val intent= Intent(this,GalleryActivity::class.java)
      intent.putExtra("IMAGE_URI", galleryUri)
      startActivity(intent)    // inflates activity_gallery.xml
   }
}

然后,在您的 GalleryActivity.kt

class GalleryActivity : AppCompatActivity() {
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gallery)

        val galleryUri = getIntent().getStringExtra("GALLERY_URI")
        val iAdapter=PageAdapter(this,galleryUri) // creating object with params
        viewPager.adapter=iAdapter 
     overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right)
        galleryBack.setOnClickListener {
            finish()
            overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right)
        }
    }
}

试试看。