代码如下:
private lateinit var context: Context
private fun getSharedPreferences(spFileName: String): SharedPreferences {
return context.getSharedPreferences(spFileName, Context.MODE_PRIVATE)
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zego_wb, PID: 7324
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zego_wb/com.example.zego_wb.activity.LoginActivity}: kotlin.UninitializedPropertyAccessException: lateinit property context 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)
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)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property context has not been initialized
at com.example.zego_wb.util.SharedPreferencesUtil$Companion.getSharedPreferences(SharedPreferencesUtil.kt:26)
at com.example.zego_wb.util.SharedPreferencesUtil$Companion.getLastJoinID(SharedPreferencesUtil.kt:80)
at com.example.zego_wb.activity.LoginActivity.initView(LoginActivity.kt:35)
at com.example.zego_wb.activity.LoginActivity.onCreate(LoginActivity.kt:29)
application 这个类 不能使用kotlin的方法进行代码编写。要使用正常的java模式进行编写,否则你要把检查初始化这个钩去掉,不然会报错