首页 > 解决方案 > 使用 Gson 将 VideoView 对象解析为 json

问题描述

我想将 VideoView 对象解析为 json,以便可以使用作业参数将其传递给 Main Activity 类以外的其他线程。

我的代码如下所示:

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

但它会抛出这样的错误:

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

我该如何解决?谢谢!

PS:我需要将其解析为 json 的原因是 jobparameters 不接受字符串、int 等以外的数据类型。我使用以下链接中的代码作为此解决方案的参考。Android JobScheduling - 我需要将一个对象传递给我的工作,但如何?

标签: androidjsongsonandroid-videoviewjob-scheduling

解决方案


推荐阅读