【问题标题】:kotlin.TypeCastException: null cannot be cast to non-null type android.widget.TextViewkotlin.TypeCastException: null 不能转换为非 null 类型 android.widget.TextView
【发布时间】:2019-09-27 09:37:25
【问题描述】:

我正在尝试在 kotlin 中编写我的应用程序,但我收到了 null cannot be casted to non-null type 并且它强制我的应用程序关闭。我试图引用其他 stackoverflow,他们在其中移动了 init,但我当前的代码中没有任何 init 部分。任何帮助将不胜感激。

reminder_fragment.kt


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val v = inflater.inflate(R.layout.reminder_fragment, container, false)

        //getting recyclerview from xml
        val recyclerView = v.findViewById(R.id.reminderrecycler) as RecyclerView

        //adding a layoutmanager
        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL ,false)

        //crating an arraylist to store users using the data class user
        val reminderlist = ArrayList<reminders>()

        //adding some dummy data to the list
        reminderlist.add(reminders("Belal Khan"))
        reminderlist.add(reminders("Ramiz Khan"))
        reminderlist.add(reminders("Faiz Khan"))
        reminderlist.add(reminders("Yashar Khan"))

        //creating our adapter
        val adapter = CustomAdapter(reminderlist)

        //now adding the adapter to recyclerview
        recyclerView.adapter = adapter
        return v;
    }
}

MainActivity.kt


class MainActivity : AppCompatActivity() {

    val manager = supportFragmentManager

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_reminders -> {
                createReminderFragment()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bottom_navigation.setItemIconTintList(null);
        createReminderFragment()
        bottom_navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }

    /*private fun replaceFragment(fragment: Fragment) {
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentplaceholder, fragment)
        fragmentTransaction.commit();
    }*/

    fun createReminderFragment() {
        val transaction = manager.beginTransaction()
        val fragment = reminder_fragment()
        transaction.replace(R.id.fragmentplaceholder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }


}

reminder_fragment.xml


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".reminder_fragment"
    android:orientation="vertical">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/reminderrecycler"/>



</LinearLayout>

CustomAdapter.kt


class CustomAdapter(val reminderlist: ArrayList<reminders>) :
    RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.reminder_fragment, parent, false)
        return ViewHolder(v);
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItems(reminderlist[position])
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(reminders: reminders) {
            val medicineText = itemView.findViewById(R.id.medicineText) as TextView
            medicineText.text = reminders.medicineN

        }

    }
}

【问题讨论】:

  • 分享你的崩溃日志。
  • 您可以只使用v.reminderrecycler 而不是findViewById(...) as RecyclerView。您还可以使用 app:layoutManagerandroid:orientation 以 XML 格式设置 LayoutManager

标签: android android-fragments kotlin android-recyclerview


【解决方案1】:

你没有很好地实现你的RecyclerView。 RecyclerView items 必须有一个单独的布局。 你设置你的 RecyclerView 项目布局,你的 RecylerView 现在在其中声明的布局。

因此 android 无法找到您的带有 Id MedicineText 的 TextView

我建议你这样做:

1) 为您的 RecyclerView 项目创建一个单独的布局并将其充气到您的持有人,如下所示:

val v = LayoutInflater.from(parent.context).inflate(R.layout.recylerView_item, parent, false)

2) 在 RecylerView 项目布局中放入一个 id 为 MedicineText 的 TextView

3) 使用 ?声明 TextView 时处理可能的 NullPonterException 的符号

    val medicineText = itemView.findViewById(R.id.medicineText) as? TextView

【讨论】:

  • 我试试看,谢谢!我会告诉你结果:)
  • 您好抱歉让您久等了! @alireza-bideli,它终于起作用了 :) 你是救生员!
猜你喜欢
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
相关资源
最近更新 更多