今天我试着运行我的应用程序,但有一个问题,我不知道它是.昨天工作正常.
如果我注解了所指示的行,应用程序将完美运行
谢谢你的帮助。
这是错误:
FATAL EXCEPTION: main
Process: com.example.tribuum_app_2022, PID: 7542
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tribuum_app_2022/com.example.tribuum_app_2022.ui.view.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.- wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException
at com.example.tribuum_app_2022.ui.view.MainActivity.validateUserLogin(MainActivity.kt:37)
at com.example.tribuum_app_2022.ui.view.MainActivity.onCreate(MainActivity.kt:30)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.- wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
存在错误的行
private fun validateUserLogin(){
val guardarEnXml = getSharedPreferences("datos_usuario_login", MODE_PRIVATE)
txtUserLoginIdShare = guardarEnXml.getString("id", null)
txtUserLoginPassShare = guardarEnXml.getString("pass", null)
Line 37: userLogin = UserLogin(txtUserLoginIdShare!!,txtUserLoginPassShare!!)
userViewModel.loginUser(userLogin)
}
对象的类:
package com.example.app_2022.domain.model
import com.example.app_2022.data.model.UserLoginModel
data class UserLogin(var id: String?, var pass: String?)
fun UserLoginModel.toDomain() = UserLogin(id,pass)
改变对象类型的其他类:
package com.example.app_2022.data.model
import com.example.app_2022.domain.model.UserLogin
import com.google.gson.annotations.SerializedName
data class UserLoginModel(
@SerializedName("idUsuario") var id: String?,
@SerializedName("contrasena") val pass: String?
)
fun UserLogin.toDatabase() = UserLoginModel(id = id, pass = pass)
1条答案
按热度按时间kgsdhlau1#
logcat表示在第37行有一个
NullPointerException
,所以txtUserLoginIdShare
和txtUserLoginPassShare
可以为空,因为SharedPreferences
中的所有值都可以为空,所以应该在使用前检查它,而不是使用!!
强制它为NotNull
。