将SparseArray存储在JSON中并使用

时间:2018-07-08 21:25:24

标签: java android arrays android-studio

当我尝试使用JSON中存储的SparseArray时,我的应用程序崩溃

当从主类(VeryBrowser.java,主webview)开始下载时,我将此函数称为VeryDownloads类:

    public void addDownload(String filePath, String fileName, long length, String date) {
        SharedPreferences prefs = VeryBrowser.getAppContext().getSharedPreferences("fr.zoldac.veryleaks_downloadslist", MODE_PRIVATE);
        String hashDownloads = prefs.getString("hashDownloads", "empty");
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();

        if(hashDownloads.equals("empty")) {
            SparseArray<String[]> sparseArray = new SparseArray<>();

            String[] list = new String[4];

            double fileLengthInKB = length / 1024;
            double fileLengthInMB = fileLengthInKB / 1024;

            list[0] = filePath;
            list[1] = fileName;
            list[2] = String.valueOf(String.format(Locale.FRANCE, "%.2f", fileLengthInMB));
            list[3] = date;

            sparseArray.put(0, list);

            String hashSparseArray = gson.toJson(sparseArray);
            prefs.edit().putString("hashString", hashSparseArray).apply();
        } else {
            Type type = new TypeToken<SparseArray<String[]>>(){}.getType();

            SparseArray<String[]> sparseArray = gson.fromJson(hashDownloads, type);

            String[] list = new String[4];

            double fileLengthInKB = length / 1024;
            double fileLengthInMB = fileLengthInKB / 1024;

            list[0] = filePath;
            list[1] = fileName;
            list[2] = String.valueOf(String.format(Locale.FRANCE, "%.2f", fileLengthInMB));
            list[3] = date;

            sparseArray.put(sparseArray.size(), list);

            String hashSparseArray = gson.toJson(sparseArray);
            prefs.edit().putString("hashString", hashSparseArray).apply();
        }
    }

但是当我在SharedPreferences中获取值时,就会出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: fr.zoldac.veryleaks, PID: 26508
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.zoldac.veryleaks/fr.zoldac.veryleaks.VeryDownloads}: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String[]
                      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.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String[]
                      at fr.zoldac.veryleaks.VeryDownloads.onCreate(VeryDownloads.java:124)
                      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)

VeryDownloads.java

    SharedPreferences prefs = VeryBrowser.getAppContext().getSharedPreferences("fr.zoldac.veryleaks_downloadslist", MODE_PRIVATE);
    String hashDownloads = prefs.getString("hashString", "empty");

    SparseArray<String[]> sparseArray;
    Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();

    if(!hashDownloads.equals("empty")) {
        Type type = new TypeToken<SparseArray<String[]>>(){}.getType();

        sparseArray = gson.fromJson(hashDownloads, type);
    } else {
        sparseArray = new SparseArray<>();

        String[] list = new String[4];

        list[0] = "";
        list[1] = "Aucun téléchargement";
        list[2] = "0.00MB";
        list[3] = "Vide";

        sparseArray.put(0, list);
    }

    ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();

    HashMap<String,String> item;

    for(int i = 0, sSize = sparseArray.size(); i < sSize; i++) {
        String[] strings = sparseArray.valueAt(i);

        item = new HashMap<>();
        item.put("title", strings[1]);
        item.put("date", strings[2] + "MB - " + strings[3]);
        arrayList.add(item);
    }

此行:String[] strings = sparseArray.valueAt(i);使应用程序崩溃

0 个答案:

没有答案