android - 当我尝试在其上保存信息时,json arrayList 出现问题
问题描述
所以我有这个应用程序,它从我的主要活动中获取来自 2 编辑文本的信息到我的第二个活动,我将它保存在 json 中,使用该信息创建一个数组列表并将其显示在 ListView 上。但是当我尝试在应用程序上保存任何信息时。我得到了这个logcat:
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:939)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.example.alexj.ejercicio6.Journal.fromJson(Journal.java:43)
at com.example.alexj.ejercicio6.ListViewActivity.onCreate(ListViewActivity.java:34)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
我遇到麻烦的代码就是这个。
public class ListViewActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
List<Journal> journalList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
sharedPreferences = getSharedPreferences("Journal", MODE_PRIVATE);
String json = sharedPreferences.getString("journal", "");
Journal journals = new Journal();
if (json !=null && !TextUtils.isEmpty(json)){
Type type = new TypeToken<List<Journal>>(){}.getType();
journalList = journals.fromJson(json, type);
}
Intent intent = getIntent();
String newPlace= intent.getStringExtra("newPlace");
String newDescription = intent.getStringExtra("newDescription");
if(journalList == null){
journalList = new ArrayList<>();
}
if(newPlace != null && !TextUtils.isEmpty(newPlace) && newDescription !=null &&
!TextUtils.isEmpty(newDescription)){
boolean listFound = false;
for (Journal journal1 : journalList){
if (journal1.getPlace().equalsIgnoreCase(newPlace)){
listFound = true;
}
}
if (listFound){
Toast.makeText(this, "You already wrote about that places", Toast.LENGTH_SHORT).show();
} else {
journalList.add(new Journal(newPlace, newDescription));
json = journals.toJson(journalList);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("journal", json);
editor.apply();
}
}
ListView list = findViewById(R.id.list);
MyAdapter myAdapter = new MyAdapter(journalList, this);
list.setAdapter(myAdapter);
}
public void back (View view) {
finish();
}
}
这是我的课堂日记,我在那里做我的 json 和 gson。
public class Journal {
private String place;
private String description;
public Journal(String place, String description){
this.place = place;
this.description = description;
}
public Journal() {
}
public String getPlace(){
return place;
}
public String getDescription(){
return description;
}
public String toJson(List<Journal> journalList){
Gson gson = new Gson();
String json = gson.toJson(journalList);
return json;
}
public List<Journal> fromJson(String json, Type type){
Gson gson = new Gson();
List<Journal> journals = gson.fromJson(json, type);
return journals;
}
}
谢谢你。
解决方案
推荐阅读
- visual-studio-code - 如何在另存为对话框中添加新的文件类型过滤器
- c - mac的多点触控代码
- scala - 将异常结果转换为迭代器
- swift - 可以创建从视图控制器的角落出现的 UITableView
- c - “使用带二进制位运算符的有符号整数操作数” - 使用无符号短时
- c# - 设置 ShowDialog 的 UIaccess 更改行为
- node.js - 可以在 package.json 的 devdependencies 中包含 npm 吗?
- google-bigquery - 如何从 BigQuery 的公共数据集中找到每个女性名字在 1910 年的最高出现率和相应状态
- regex - XQuery MarkLogic 中的模式或格式匹配
- asp.net - Web.config/app.config 元素 configSections 在 DotNet4.5 中是否已弃用?