0

In the below code, I am trying to convert activity to a JSON object so I can pass such an object to a service via intent. please refer to the code posted in code_1 and code_2 sections. However, when the app is executed, I receive the below-posted error.

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

please let me know why I receive such error and how to solve it.

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);
        }
    });

error:

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
Udara Abeythilake
  • 1,215
  • 1
  • 20
  • 31
user10776303
  • 241
  • 6
  • 16
  • paste your pojo/bean class where field is there " mChangingConfigurations" – hemen Jun 15 '19 at 09:00
  • Converting entire activity to JSON is not feasible. Intents only carry a small bundle of data, you must decide what you actually need to send to service and include it in intent extras. – Pawel Jun 15 '19 at 09:07

1 Answers1

3

It looks you need context inside service, Therefore you are trying to convert Activity object to JSON object (as per my understanding). The solution is just to use this keyword only inside service Context context = this; because Service is made up of Context only, Maybe more details can help you Get Context in a Service

Ashish
  • 997
  • 6
  • 20