练习聊天界面 ,添加点击事件后启动闪退

还没在AppCompatActivity后面添加View.OnClickListener重写onClick之前,能正常启动界面

img

不断尝试,最后分别在override fun onCreate, override fun onClick中添加对应recyc布局和按键的R.id资源才行,虽然能运行,但是感觉还不太对劲。重复使用太多了把

MainActivity.kt:

package android.example.uibestpracticc

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private val msgList = ArrayList<Msg>()
    private var adapter: MsgAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val send: Button = findViewById(R.id.send)
        val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
        initMsg()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = MsgAdapter(msgList)
        send.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        val inputText: EditText = findViewById(R.id.inputText)
        val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
        val send: Button = findViewById(R.id.send)

        when (v) {
            send -> {
                val content = inputText.text.toString()
                if (content.isNotEmpty()) {
                    val msg = Msg(content, Msg.TYPE_SENT)
                    msgList.add(msg)
                    adapter?.notifyItemInserted(msgList.size - 1)
                    recyclerView.scrollToPosition(msgList.size - 1)
                    inputText.setText("")
                }
            }
        }
    }

    private fun initMsg() {
        val msg1 = Msg("hello mmy", Msg.TYPE_RECEIVED)
        msgList.add(msg1)
        val msg3 = Msg("hello who a you", Msg.TYPE_SENT)
        msgList.add(msg3)
        msgList.add(Msg("启动正常,按键没响应", Msg.TYPE_SENT))
    }
}

MsgAdapter.kt:

package android.example.uibestpracticc

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MsgAdapter(private val msgList: List<Msg>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    inner class LeftViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val leftMsg: TextView = view.findViewById(R.id.leftMsg)
    }

    inner class RightViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val rightMsg: TextView = view.findViewById(R.id.rightMsg)
    }

    override fun getItemViewType(position: Int): Int {
        return msgList[position].type
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
        if (viewType == Msg.TYPE_RECEIVED) {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.msg_left_item, parent, false)
            LeftViewHolder(view)
        } else {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.msg_right_item, parent, false)
            RightViewHolder(view)
        }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val msg = msgList[position]
        when (holder) {
            is LeftViewHolder -> holder.leftMsg.text = msg.content
            is RightViewHolder -> holder.rightMsg.text = msg.content
            else -> throw  IllegalArgumentException()
        }
    }

    override fun getItemCount() = msgList.size
}

Msg.kt:

package android.example.uibestpracticc

class Msg(val content: String, val type: Int) {
    companion object {
        const val TYPE_RECEIVED = 0
        const val TYPE_SENT = 1
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/inputText"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="16dp"
            android:autofillHints=""
            android:hint="@string/type_edit"
            android:inputType="textMultiLine"
            android:minHeight="48dp"
            android:textColorHint="@color/black" />

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:backgroundTint="#808080"
            android:hint="@string/send"
            android:textColorHint="#000000"
            tools:ignore="TextContrastCheck" />

    </LinearLayout>

</androidx.appcompat.widget.LinearLayoutCompat>
运行结果及报错内容