赞
踩
“?"来做安全调用,如果对象为空,结果也为null
“!!”对象为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); }
打印日志如下:
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)
果变量不为null的话,我们使用变量,否则,我们使用另外一个值
Log.i(TAG,"test = "+(test?.length?:-1));
打印:
2023-02-27 17:01:13.186 21748-21748/com.kotlin.demo I/MainActivity: test = -1
当一个变量可能为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);
}
日志:
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
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);
}
报错:
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。