[英]Android App crashes when using SharedPreferences.contains()
[英]Android App crashes when using SharedPreferences
我制作了這個應用程序來獲取朋友列表並將其存儲在 sharedPreferences 中。 當我運行應用程序時,它不會顯示任何錯誤並崩潰。
這是我的代碼
package com.abhishek.sharedpreferances;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText editTextFriends = (EditText) findViewById(R.id.editTextFriends);
Button buttonSave = (Button) findViewById(R.id.buttonSave);
TextView textViewFriends = (TextView) findViewById(R.id.textViewFriends);
SharedPreferences sharedPreferences = this.getSharedPreferences("com.abhishek.sharedpreferances", Context.MODE_PRIVATE);
ArrayList<String> friends = new ArrayList<>();
ArrayList<String> newFriends = new ArrayList<>();
public void save(View view) throws IOException {
friends.add(editTextFriends.getText().toString());
//ObjectSerializer.serialize(friends);
try {
sharedPreferences.edit().putString("friends",ObjectSerializer.serialize(friends)).apply();
} catch (Exception e) {
e.printStackTrace();
}
newFriends = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("friends",ObjectSerializer.serialize(new ArrayList<String>())));
assert newFriends != null;
textViewFriends.setText(newFriends.toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我創建了 ObjectSerializer java class 以將數組列表存儲到 sharedPreferences 到應用程序中我提供了下面的 class 請檢查一下
這是我的 ObjectSerializer java class
package com.abhishek.sharedpreferances;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectSerializer {
public static String serialize(Serializable obj) throws IOException {
if (obj == null) return "";
try {
ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
objStream.writeObject(obj);
objStream.close();
return encodeBytes(serialObj.toByteArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object deserialize(String str) throws IOException {
if (str == null || str.length() == 0) return null;
try {
ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
ObjectInputStream objStream = new ObjectInputStream(serialObj);
return objStream.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
public static byte[] decodeBytes(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i+=2) {
char c = str.charAt(i);
bytes[i/2] = (byte) ((c - 'a') << 4);
c = str.charAt(i+1);
bytes[i/2] += (c - 'a');
}
return bytes;
}
}
在 putString 和 getString 可以從 SharedPreferences 中獲取保存的字符串之后,請嘗試保留sharedPreferences.edit()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.