当前位置:   article > 正文

Android Kotlin学习(四)- NULL空安全_kotling 不能为null !!

kotling 不能为null !!

Android Kotlin学习(四)- NULL空安全


Java出现最多的异常不知道是不是空指针异常,Kotlin的空安全避免了空指针异常导致程序崩溃

?和!!

  1. “?"来做安全调用,如果对象为空,结果也为null

  2. “!!”对象为null,回像JAVA一样报出空指针异常

     // test可以为null
     var test:String?=null;
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
             startTest();
         }
    
     private fun startTest() {
     	// 这样写编译会报错,因为test可能为null
    	//Log.i(TAG,"test = "+test.length);
    	// 这样写编译通过,会返回null
    	Log.i(TAG,"test = "+test?.length);
         //这样也会返回null
         Log.i(TAG,"test = "+test);
         //test null时,会抛出空指针异常
         Log.i(TAG,"test = "+test!!);
         test = "Hello";
         Log.i(TAG,"test1 = "+test);
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

打印日志如下:

2023-02-27 16:34:00.383 21084-21084/com.kotlin.demo I/MainActivity: test = null
2023-02-27 16:34:00.383 21084-21084/com.kotlin.demo D/AndroidRuntime: Shutting down VM
2023-02-27 16:34:00.388 21084-21084/com.kotlin.demo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kotlin.demo, PID: 21084
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kotlin.demo/com.kotlin.demo.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Elvis

果变量不为null的话,我们使用变量,否则,我们使用另外一个值

Log.i(TAG,"test = "+(test?.length?:-1));
  • 1

打印:

2023-02-27 17:01:13.186 21748-21748/com.kotlin.demo I/MainActivity: test = -1
  • 1

let

当一个变量可能为null时,可以通过let函数来处理,null时则不会被执行:

	Log.i(TAG,"test = "+(test?.length?:-1));
    test?.let {
         //此时test null,所以不会执行
        Log.i(TAG,"test == "+test?.length);
    }
    test = "Hello";
    test?.let {
        Log.i(TAG,"test === "+test?.length);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

日志:

2023-02-27 17:27:29.143 22158-22158/com.kotlin.demo I/MainActivity: test = -1
2023-02-27 17:27:29.143 22158-22158/com.kotlin.demo I/MainActivity: test === 5
  • 1
  • 2

lateinit

lateinit延时初始化,默认非null,使用时要保证不为null,否则报错:

	lateinit var test:String;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        startTest();
    }

    private fun startTest() {
        Log.i(TAG,"test = "+test);
        Log.i(TAG,"test = "+test!!);
        test = "Hello";
        Log.i(TAG,"test1 = "+test);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

报错:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kotlin.demo/com.kotlin.demo.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property test has not been initialized
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/992349
推荐阅读
相关标签
  

闽ICP备14008679号