【问题标题】:how to haveinstance of activity converted to gson如何将活动实例转换为 gson
【发布时间】:2019-06-15 08:55:00
【问题描述】:

在下面的代码中,我尝试将活动转换为 JSON 对象,以便可以通过意图将此类对象传递给服务。请参阅 code_1 和 code_2 部分中发布的代码。但是,当应用程序执行时,我收到 下面发布的错误。

    java.lang.IllegalArgumentException: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations

请告诉我为什么会收到这样的错误以及如何解决。

code_1

public class JsonConverterAndroidOS implements IConvertToJson {
    @Override
    public String convertToJosn(Context ctx) {
    Gson gson = new Gson();
    Log.i("", "gson.toJson(ctx) : " + gson.toJson(ctx));
    return gson.toJson(ctx);
    }
}

code_2

this.mBtn_Start_GPS_Readings.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "GPS_READINGS", Toast.LENGTH_SHORT).show();
            Intent intentStartGPSService = new Intent(getApplicationContext(), GPSService.class);
            intentStartGPSService.setAction(ACTION_START_GPS_READING);

            //activity to json conversion
            IConvertToJson converter = new JsonConverterAndroidOS();
            String convertedToJson = converter.convertToJosn(MainActivity.this);
            intentStartGPSService.putExtra(KEY_MAIN_ACTIVITY_INSTANCE, convertedToJson);
            //startService(intentStartGPSService);
        }
    });

错误

2019-06-15 10:45:12.898 1983-2013/com.example.gps_v10 E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-06-15 10:45:12.899 1983-2013/com.example.gps_v10 E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-06-15 10:45:26.411 1983-1983/com.example.gps_v10 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gps_v10, PID: 1983
java.lang.IllegalArgumentException: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.Gson.toJson(Gson.java:695)
    at com.google.gson.Gson.toJson(Gson.java:682)
    at com.google.gson.Gson.toJson(Gson.java:637)
    at com.google.gson.Gson.toJson(Gson.java:617)
    at com.example.gps_v10.JsonConverterAndroidOS.convertToJosn(JsonConverterAndroidOS.java:12)
    at com.example.gps_v10.MainActivity$1.onClick(MainActivity.java:49)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2019-06-15 10:45:26.422 1983-1990/com.example.gps_v10 I/zygote: Do partial code cache collection, code=30KB, data=26KB
2019-06-15 10:45:26.422 1983-1990/com.example.gps_v10 I/zygote: After code cache collection, code=30KB, data=26KB
2019-06-15 10:45:26.422 1983-1990/com.example.gps_v10 I/zygote: Increasing code cache capacity to 128KB

【问题讨论】:

  • 将 pojo/bean 类粘贴到字段所在的位置“mChangingConfigurations”
  • 将整个活动转换为 JSON 是不可行的。 Intent 只携带一小部分数据,您必须确定实际需要发送到服务的内容并将其包含在 Intent Extras 中。

标签: android json gson


【解决方案1】:

看起来您需要服务内部的上下文,因此您正在尝试将 Activity 对象转换为 JSON 对象(根据我的理解)。 解决方案是仅在服务内部使用 this 关键字 Context context = this;因为Service只由Context组成,也许更多细节可以帮助你Get Context in a Service

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多