Android 为什么每当我尝试向RecyclerView添加数据类元素时,应用程序就会崩溃
我正在学习如何在大学作业中使用kotlin,我们需要使用GSON保存数据类,并将数据类转换为字符串,使用SharedReferences保存字符串。调用保存的对象后,当我尝试将数据传递给recyclerview时,应用程序崩溃 这是保存对象的位置Android 为什么每当我尝试向RecyclerView添加数据类元素时,应用程序就会崩溃,android,kotlin,android-recyclerview,gson,sharedpreferences,Android,Kotlin,Android Recyclerview,Gson,Sharedpreferences,我正在学习如何在大学作业中使用kotlin,我们需要使用GSON保存数据类,并将数据类转换为字符串,使用SharedReferences保存字符串。调用保存的对象后,当我尝试将数据传递给recyclerview时,应用程序崩溃 这是保存对象的位置 package com.example.juliaojonah_hci_outcome2_v1 import android.content.Context import android.content.Intent import android.o
package com.example.juliaojonah_hci_outcome2_v1
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_main.*
import java.security.spec.PSSParameterSpec
import java.text.DateFormat
import java.util.*
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dateTime = Calendar.getInstance().time
val dateFormatted = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(dateTime)
val dateTextView : TextView = findViewById(R.id.textDate) as TextView
dateTextView.setText(dateFormatted)
}
fun secondActivity(view: View){
val intent = Intent(this, passwords::class.java)
startActivity(intent)
}
fun randomise(view: View){
val passwordSet1 = Random.nextInt(0,99)
val passwordSet2 = Random.nextInt(0,99)
val passwordSet3 = Random.nextInt(0,99)
val passwordSet4 = Random.nextInt(0,99)
val editText1 = findViewById<EditText>(R.id.password1)
editText1.setText(passwordSet1.toString())
val editText2 = findViewById<EditText>(R.id.password2)
editText2.setText(passwordSet2.toString())
val editText3 = findViewById<EditText>(R.id.password3)
editText3.setText(passwordSet3.toString())
val editText4 = findViewById<EditText>(R.id.password4)
editText4.setText(passwordSet4.toString())
}
fun save(view: View){
val sharedPrefsCounter = getSharedPreferences("passwordAppCounter", Context.MODE_PRIVATE)
var i = sharedPrefsCounter.getInt("counter", 0)
if (i == 0){
val editorCounter = sharedPrefsCounter.edit()
editorCounter.putInt("counter", 1)
editorCounter.apply()
i = 1
}
val dateTime = Calendar.getInstance().time
val dateFormatted = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(dateTime)
val description = descriptionName.text.toString().trim()
val password = password1.text.toString().trim() + "-" + password2.text.toString().trim() + "-" + password3.text.toString().trim() + "-" + password4.text.toString().trim()
if (description == "" || password == "---"){
Toast.makeText(this, "Please Enter Proper Value", Toast.LENGTH_LONG).show()
} else {
val date = dateTime.toString()
val data = PasswordCluster(description, password, date)
val jsonPassword = Gson().toJson(data)
val sharedPrefsPasswords =
getSharedPreferences("passwordAppPasswords", Context.MODE_PRIVATE)
val editorPassword = sharedPrefsPasswords.edit()
editorPassword.putString("passwordObject" + i, jsonPassword)
editorPassword.apply()
Toast.makeText(this, "Data Saved", Toast.LENGTH_LONG).show()
val editorCounter = sharedPrefsCounter.edit()
editorCounter.putInt("counter", i + 1)
editorCounter.apply()
}
}
}
这就是我尝试运行粘贴对象数据的活动时出现的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.juliaojonah_hci_outcome2_v1, PID: 24773
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.juliaojonah_hci_outcome2_v1/com.example.juliaojonah_hci_outcome2_v1.passwords}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.juliaojonah_hci_outcome2_v1.PasswordCluster.getDescription()' on a null object reference
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: Attempt to invoke virtual method 'java.lang.String com.example.juliaojonah_hci_outcome2_v1.PasswordCluster.getDescription()' on a null object reference
at com.example.juliaojonah_hci_outcome2_v1.passwords.onCreate(passwords.kt:52)
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)
感谢您的帮助您从SharedReference获得的
objectString
的默认值为空字符串。因此,如果您尝试检索的共享首选项还不存在,则将“
传递给Gson.fromJson()
。然后它返回null,因为它无法将其解析为任何内容
因为Gson是一个Java库,所以Kotlin不会对返回值强制null安全性 那么我如何确保它不是空的呢?从共享首选项中提取对象是否工作不正常?如果为空,您可以将其更改为
“”
。我似乎记得,如果您存储空字符串首选项,当您稍后检索它时,它是空的。因为它已经存在,所以得到的是null,而不是传递给getString()
的默认值。它起作用了。谢谢我没有意识到修复它只会修复一切。我将数据类的测试版本转换为json字符串,并将其作为默认值传递。我检查了SharedReferences,文件确实存在,所以我不明白为什么它说它是空的
package com.example.juliaojonah_hci_outcome2_v1
data class PasswordCluster(var description: String, var passwordCluster: String, var dateTime: String)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.juliaojonah_hci_outcome2_v1, PID: 24773
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.juliaojonah_hci_outcome2_v1/com.example.juliaojonah_hci_outcome2_v1.passwords}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.juliaojonah_hci_outcome2_v1.PasswordCluster.getDescription()' on a null object reference
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: Attempt to invoke virtual method 'java.lang.String com.example.juliaojonah_hci_outcome2_v1.PasswordCluster.getDescription()' on a null object reference
at com.example.juliaojonah_hci_outcome2_v1.passwords.onCreate(passwords.kt:52)
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)