java.lang.SecurityException:无法访问java.lang.reflect.Method构造函数

时间:2018-02-13 10:09:20

标签: java android gson

我正在使用Gson版本2.8.2

我使用了以下代码

public void saveObject(View view){
        SharedPreferences prefs = this.getSharedPreferences(
                "com.example.app", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        Gson gson = new Gson();
        String str = gson.toJson(view, View.class);
        editor.putString("text", str);
        editor.commit();
}

但我收到如下错误

Process: com.example.amitsiddhpura.paperview, PID: 17252
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.amitsiddhpura.paperview/com.example.amitsiddhpura.paperview.MainActivity}: **java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible**
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                                      Caused by: java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible
                                                                                         at java.lang.reflect.AccessibleObject.setAccessible0(AccessibleObject.java:131)
                                                                                         at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:115)
                                                                                         at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:101)
                                                                                         at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:83)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:99)
                                                                                         at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                                         at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                                         at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
                                                                                         at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                                         at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                                         at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
                                                                                         at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
                                                                                         at com.google.gson.Gson.getAdapter(Gson.java:423)
                                                                                         at com.google.gson.Gson.toJson(Gson.java:661)
                                                                                         at com.google.gson.Gson.toJson(Gson.java:648)
                                                                                         at com.google.gson.Gson.toJson(Gson.java:603)
                                                                                         at com.example.amitsiddhpura.paperview.MainActivity.saveObject(MainActivity.java:34)
                                                                                         at com.example.amitsiddhpura.paperview.MainActivity.onCreate(MainActivity.java:23)
                                                                                         at android.app.Activity.performCreate(Activity.java:6999)
                                                                                         at android.app.Activity.performCreate(Activity.java:6990)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                         at android.os.Looper.loop(Looper.java:164) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

2 个答案:

答案 0 :(得分:3)

您正在尝试保存包含无法序列化的上下文的View类。 Read Here.

答案 1 :(得分:0)

你的第一个参数视图在toJson方法中是错误的。来自文档Class GSON。通过适当的参数,它将起作用

toJson(JsonElement jsonElement)

  

将JsonElements树转换为等效的JSON   表示。

void toJson(JsonElement jsonElement,Appendable writer)

  

为JsonElements树写出等效的JSON。

void toJson(JsonElement jsonElement,JsonWriter writer)

  

将jsonElement的JSON写入writer。

String toJson(Object src)

  

此方法将指定对象序列化为等效的Json   表示。

void toJson(Object src,Appendable writer)

  

此方法将指定对象序列化为等效的Json   表示。

String toJson(Object src,Type typeOfSrc)

  

此方法序列化指定的对象,包括   泛型类型,等效于Json表示。

void toJson(Object src,Type typeOfSrc,Appendable writer)

  

此方法序列化指定的对象,包括   泛型类型,等效于Json表示。

void toJson(Object src,Type typeOfSrc,JsonWriter writer)

  

将typeOfSrc类型的src的JSON表示写入writer。