美文网首页Note_Android
Kotlin使用总结

Kotlin使用总结

作者: Qin0821 | 来源:发表于2017-07-14 17:29 被阅读0次
  1. Kotlin优化了复杂界面大段大段的findViewById代码,取而代之的是导包形式:
    import kotlinx.android.synthetic.main.布局文件.*
    之后直接根据控件的ID获取控件对象,如:
<Button
        android:id="@+id/QRCode"
        android:text="QRCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

直接调用,不需要额外的代码

QRCode.setOnClickListener(this)

大概是在Kotlin1.3.2版本以前,当一个xml文件中include其他xml文件时,通过将子xml文件import进去,也可以直接根据id获得控件对象。当我更新到1.3.2版本后,根据id得到的控件并不是界面显示的那个对象,使用时就会报KotlinNullPointException。
通过给include的xml添加id

<include
                    android:id="@+id/mLayout_home_to_audit_info"
                    layout="@layout/item_home_today_info" />

在父view中findViewById获取子view对象(fragment中)

private fun initView() {

        mLayout_home_to_audit_info = root!!.findViewById(R.id.mLayout_home_to_audit_info)
   
    }

之后使用中才不会空指针异常

 Layout_home_to_audit_info!!.visibility = View.GONE `

相关文章

  • Kotlin使用总结

    后端 Java 项目也可引入 Kotlin 优点:1,无缝引入到现有 Java 项目,只看了半天文档就上手了,并且...

  • Kotlin使用总结

    Kotlin优化了复杂界面大段大段的findViewById代码,取而代之的是导包形式:import kotlin...

  • kotlin 中的异常

    文章目录 前言 kotlin 异常的简介 kotlin 异常的使用 总结 前言 java 中的异常只要 try/c...

  • Kolin学习笔记

    本人于2018年开始使用kotlin开发项目,现将使用过程中总结出来的笔记形成系列文章,以供大家参考。kotlin...

  • Kotlin基础教程(一)

    很久没有对一段时间的工作学习进行总结,所以想就近一个月使用kotlin编程,进行简单的总结.首先,kotlin真的...

  • Kotlin中let, with, run, apply, al

    Kotlin中let, with, run, apply, also方法的使用总结 调用方式传递参数(it/thi...

  • kotlin TTS使用总结

    package com.ljb.mvp.kotlin.utils import android.annotatio...

  • Alamofire Get、Post请求的简单封装

    封装类 使用方式 3.总结 字符串拼接方式和 kotlin 有点像

  • Android Kotlin 反射使用 (null receiv

    最近在使用Kotlin反射遇到些问题,总结一下:报错:java.lang.NullPointerException...

  • Kotlin的爽歪歪之处<一>

    Kotlin是啥我就几句话总结下:类似IOS的Swift,却比Swift成熟稳重,使用起来一样的爽。Kotlin可...

网友评论

    本文标题:Kotlin使用总结

    本文链接:https://www.haomeiwen.com/subject/vxbnhxtx.html