使用Gson类时出错[STACKOVERFLOW ERROR]

时间:2014-11-25 19:30:12

标签: android json gson

我想从我的班级中保存一个具有包名等的对象。所以我试图找到一个通过SharedPreferences保存这个对象的解决方案,并发现我可以使用Gson类将此Object变为JSON字符串。但第一次尝试失败了。我通过转换它得到一个错误。我也一步一步地调试并找到了一行:

  

String jsonapp = gson.toJson(installed_apps);

以下是整个代码:

    List<PackageInfo> apps;
    List<AppInfo> installed_apps;
    ...
            apps = getPackageManager().getInstalledPackages(0);
            installed_apps = new ArrayList<AppInfo>();  

              for(int i=0;i<apps.size();i++) 
                {
                                PackageInfo p = apps.get(i); 
                                AppInfo newInfo = new AppInfo();
                                newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
                                newInfo.pname = p.packageName;
                                newInfo.versionName = p.versionName;
                                newInfo.versionCode = p.versionCode;
                                newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
                                installed_apps.add(newInfo);
                 }

                Gson gson = new Gson();
                String jsonapp = gson.toJson(installed_apps);

这是我的班级:

class AppInfo {
        String appname = "";
        String pname = "";
        String versionName = "";
        int versionCode = 0;
        Drawable icon;

        public String get_appname () {
            return appname;
        }
    };

崩溃报告:

11-25 20:20:20.911: E/AndroidRuntime(21989): FATAL EXCEPTION: main
11-25 20:20:20.911: E/AndroidRuntime(21989): Process: com.ronssoft.JewE_lite, PID: 21989
11-25 20:20:20.911: E/AndroidRuntime(21989): java.lang.StackOverflowError
11-25 20:20:20.911: E/AndroidRuntime(21989):    at java.lang.Class.isArray(Class.java:1118)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:331)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:355)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.Gson.getAdapter(Gson.java:356)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
11-25 20:20:20.911: E/AndroidRuntime(21989):    at com.google.gson.internal.bind.R

为什么我会收到此错误,并为此找到解决方案?有任何想法吗 ?谢谢!

2 个答案:

答案 0 :(得分:2)

你在序列化Drawable吗?我不是肯定的,但似乎这可能会导致你的问题。您是否尝试从班级中删除Drawable字段?

我将Drawable作为图像保存到文件系统并保存其路径的字符串表示。

答案 1 :(得分:0)

你的循环条件for(int i=0;i<apps.size();i++)说&#34;循环遍历此列表,直到你到达它结束为止#34;。在循环内部,您将向列表中添加项目,以便您实际上永远不会到达它的末尾,从而实现堆栈溢出。

在Java中,当您以这种方式声明循环时,每次循环迭代时都会调用函数apps.size()。首先,声明一个整数并为其分配数组长度的值,然后以这种方式循环遍历数组,这是一种很好的做法。这不仅意味着您保证只能在列表中循环有限次数,而且还鼓励您更有效地编写代码。循环的一个例子是

    int size = apps.size();
    for(int i=0;i<size;i++) {
    do stuff.....
    }