【问题标题】:Type mismatch. Required: (RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?) Found: PageAdapter类型不匹配。必需:(RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?) 找到:PageAdapter
【发布时间】:2021-07-01 03:39:23
【问题描述】:

我在下面代码的PageAdapter(supportFragmentManager) 行中收到一个错误type mismatch

class PatientDetailsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patient_details)    
        viewPager.adapter = PageAdapter(supportFragmentManager) //error   
    }

}

这是我的adapter.kt

 class PageAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    override fun getCount(): Int {
        return 4
    }

    override fun getItem(position: Int): Fragment {
        when(position){
            0 -> return(Fragment1())
            1 -> return(Fragment2())
            2 -> return(Fragment3())
            3 -> return(Fragment4())
        }
    }
}

布局

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.sample.PatientDetailsActivity">


  <com.google.android.material.tabs.TabLayout
      android:id="@+id/tabLayout"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="1dp"
      android:layout_marginEnd="1dp"
      android:layout_marginBottom="1dp"
      app:layout_constraintBottom_toTopOf="@+id/viewPager"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Monday" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tuesday" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wednesday" />
  </com.google.android.material.tabs.TabLayout>

  <androidx.viewpager2.widget.ViewPager2
      android:id="@+id/viewPager"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:layout_marginStart="1dp"
      android:layout_marginEnd="1dp"
      android:layout_marginBottom="1dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android kotlin android-viewpager


    【解决方案1】:

    我相信您在 xml 布局中使用 ViewPager2 来实现 viewpager 功能。由于 ViewPager2 不能与 PagerAdapter 一起使用,它确实需要 RecyclerView 适配器。

    您的简单解决方案是使用 ViewPager 而不是 ViewPager2。

    如果您想使用 ViewPager2,您可以按照此链接中的文章了解 RecyclerViewAdapter 的实现。

    View Pager 2

    【讨论】:

      【解决方案2】:
      Type mismatch. 
      Required: (RecyclerView.Adapter<RecyclerView.ViewHolder!>?..RecyclerView.Adapter<*>?) 
      Found: PageAdapter
      

      毫无疑问,activity_patient_details 布局中的id 被错误地设置为RecyclerView 的ID,这就是错误的含义。

      它在找到 PageAdapter 时需要 ReyclerView.Adapter

      因此,您需要检查布局中ViewPager 的id 并将其设置为viewPager

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多