0

I wanted to parse a VideoView object to json so that I could pass it to a different thread other than the Main Activity class using jobparameters.

My code looks like this:

VideoView videoViewN = findViewById(R.id.video);
Gson g = new Gson();
String json = g.toJson(videoViewN); //this is the line that causes the error

PersistableBundle bundle = new PersistableBundle();
bundle.putString("VideoView", json);

JobInfo info = new JobInfo.Builder(123, componentName)
        .setPersisted(true)
        .setExtras(bundle)
        .setPeriodic(5 * 60 * 1000)
        .build();

But it throws error like this:

2020-09-08 16:49:30.408 19352-19352/com.example.ses_adplatform_test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ses_adplatform_test, PID: 19352
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ses_adplatform_test/com.example.ses_adplatform_test.MainActivity}: java.lang.IllegalArgumentException: class android.widget.MediaController declares multiple JSON fields named mContext

How do I fix this? Thank you!

PS: the reason why I need to parse it to json is that jobparameters do not accept datatypes other than string, int, etc. I used code from the following link as a reference for this solution. Android JobScheduling - I need to pass an object to my job but how?

Tina
  • 1
  • 1
  • Even if that code worked, serializing third party classes to JSON using Gson should be avoided because you are relying on their implementation details which could change at any point breaking your application. Also it appears the widget classes have a lot of fields and so even if serialization to JSON worked, the performance would likely be very bad. Maybe there is a better way to solve this (though I am not familiar with Android so I can't help with this sadly). – Marcono1234 Sep 08 '20 at 22:45
  • There's no need to do that, if you want to control the VideoView from another activity, try [control view from another Activity](https://stackoverflow.com/a/7978389/7954210), or you can use **Event Bus** – Mouaad Abdelghafour AITALI Sep 09 '20 at 00:04

0 Answers0