根据TypeScript文档:
但是,当使用-strictNullChecks 标志时, null 和 undefined 仅可分配给 void 和它们各自的类型。这有助于避免许多常见错误。如果要传递字符串,null或未定义,则可以使用联合类型 string |空|未定义。再一次,稍后会更多地讨论联合类型。
但是,当我尝试以下代码片段时:
2019-06-01 21:15:08.408 13431-13431/? E/ample.project_: Unknown bits set in runtime_flags: 0x8000
2019-06-01 21:15:08.798 13431-13453/? E/vndksupport: Could not load /vendor/lib/egl/libGLES_emulation.so from sphal namespace: dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found.
2019-06-01 21:15:08.798 13431-13453/? E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): unknown
2019-06-01 21:15:09.798 13431-13452/com.example.project_1 E/EGL_emulation: tid 13452: eglQueryString(923): error 0x3008 (EGL_BAD_DISPLAY)
2019-06-01 21:15:17.712 13431-13447/com.example.project_1 E/MediaPlayerNative: error (1, -2147483648)
2019-06-01 21:15:17.715 13431-13431/com.example.project_1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.project_1, PID: 13431 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference at
com.example.project_1.MainActivity.SoundPlay(MainActivity.java:51) at
com.example.project_1.MainActivity$2.onClick(MainActivity.java:43) at
android.view.View.performClick(View.java:7251) at
android.view.View.performClickInternal(View.java:7228) at
android.view.View.access$3500(View.java:802) at
android.view.View$PerformClick.run(View.java:27843) at
android.os.Handler.handleCallback(Handler.java:883) at
android.os.Handler.dispatchMessage(Handler.java:100) at
android.os.Looper.loop(Looper.java:214) at
android.app.ActivityThread.main(ActivityThread.java:7116) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
它没有完全按预期工作。
根据文档,我应该能够将 null 分配给void类型。有人可以消除这个疑问吗?
答案 0 :(得分:0)
该文档具有误导性,您很容易感到困惑。这里有个问题:https://github.com/microsoft/TypeScript-Handbook/issues/468