如何使用kotlin在android中初始化小部件

如何使用kotlin在android中初始化小部件,android,nullpointerexception,kotlin,kotlin-android-extensions,Android,Nullpointerexception,Kotlin,Kotlin Android Extensions,我已经开始学习在android中使用kotlin语言,并在初始化button变量时遇到问题,因为在定义变量时,当我使用null值初始化时,它要求提供一些初始值,并在oncreate函数中绑定变量 kotlin.KotlinNullPointerException 这是我的密码 class AddsFragment : Fragment() { var Add: Button = null!! override fun onCreateView(inflater: Layout

我已经开始学习在android中使用kotlin语言,并在初始化button变量时遇到问题,因为在定义变量时,当我使用null值初始化时,它要求提供一些初始值,并在oncreate函数中绑定变量

kotlin.KotlinNullPointerException

这是我的密码

class AddsFragment : Fragment() {

    var Add: Button = null!!

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val Rootview = inflater!!.inflate(R.layout.clubsfragment, null, false)
        Add = Rootview.findViewById(R.id.add) as Button
        return Rootview
    }
}

操作员检查接收器是否为空,是否为抛出KotlinNullPointerException。所以
null将始终引发异常

您可以通过以下方式实现您的目标:

  • 将属性类型设置为
    按钮?
    。在这种情况下,当访问按钮的方法时,您必须使用

    var add: Button? = null
    // Initialize it somewhere.
    
    add?.setText("Text") // Calls setText if Add != null
    add!!.setText("Text") // Throws an exception if Add == null
    
  • 使按钮成为
    lateinit
    属性

    lateinit var add: Button
    
    var add: Button by Delegates.notNull()
    
  • 使按钮成为
    notNull
    委托属性

    lateinit var add: Button
    
    var add: Button by Delegates.notNull()
    
  • 在最后两种情况下,您无法检查按钮是否为
    null
    。如果需要对变量进行
    null
    比较,请使用第一种方法


    还有另外一种方法,我不会在这个答案中详细描述。首先是使用。这是一个编译器插件,可以为您的视图生成合成属性,因此您不需要调用
    findviewbyd()
    ,并且可以使用生成的属性访问视图

    第二种方法是创建自己的委托,该委托将为您调用
    findViewById()
    。它可能看起来像这样:

    val add: Button by bindView(R.id.add)
    

    您可以在项目中找到此类委托的示例。

    您可以使用Kotlin编写helper
    bind
    函数:

    fun <T : View> Activity.bind(@IdRes res : Int) : Lazy<T> {
        @Suppress("UNCHECKED_CAST")    
        return lazy { findViewById(res) as T }
    }
    

    类MyActivity:AppCompatActivity(){
    绑定的专用val按钮(R.id.button)
    }
    
    您可以使用以下任一方法在kotlin中初始化视图:

    1。可为空的变量

    private var textView: TextView? = null
    …
    textView = findViewById(R.id.text_view) as TextView
    
    private val textView: TextView by lazy { findViewById(R.id.text_view) as TextView }
    
    2。lateinit

    private lateinit var textView: TextView
    …
    textView = findViewById(R.id.text_view) as TextView
    
    3。Delegates.notNull()

    4。惰性属性

    private var textView: TextView? = null
    …
    textView = findViewById(R.id.text_view) as TextView
    
    private val textView: TextView by lazy { findViewById(R.id.text_view) as TextView }
    
    5。黄油刀

    @BindView(R2.id.text_view)
    internal lateinit var textView: TextView
    
    private val textView: TextView by bindView(R.id.text_view)
    
    6。Kotterknife

    @BindView(R2.id.text_view)
    internal lateinit var textView: TextView
    
    private val textView: TextView by bindView(R.id.text_view)
    
    7。Kotlin Android扩展版

    private var textView: TextView? = null
    …
    textView = findViewById(R.id.text_view) as TextView
    
    private val textView: TextView by lazy { findViewById(R.id.text_view) as TextView }
    
    没有代码示例,只需添加正确的导入,并开始使用合成生成的属性,如下所示

    import kotlinx.android.synthetic.main.<layout>.*
    

    您可以在

    中检查每种方法的优缺点,那么如何初始化变量??