首页 > 解决方案 > 当我尝试在其上保存信息时,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;
}

}

谢谢你。

标签: android

解决方案


推荐阅读