Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在FUNF中实现BundleTypeAdapter_Android_Bundle_Gson - Fatal编程技术网

Android 在FUNF中实现BundleTypeAdapter

Android 在FUNF中实现BundleTypeAdapter,android,bundle,gson,Android,Bundle,Gson,因此,我希望通过实现我在 我的问题是,我不完全确定如何正确地实现这段代码。目前唯一对我有意义的方法是循环依赖 您通常如何实现新的TypeAdapter: ... GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer()); gsonBuilder.registerTypeAdapter(Location.class,

因此,我希望通过实现我在

我的问题是,我不完全确定如何正确地实现这段代码。目前唯一对我有意义的方法是循环依赖

您通常如何实现新的TypeAdapter:

...
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer());
gsonBuilder.registerTypeAdapter(Location.class, new LocationSerializer());
Gson gson = gsonBuilder.create(); 
...
或者,我希望在这种情况下:

...
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Bundle.class, new BundleTypeAdapter());
Gson gson = gsonBuilder.create();
...
我的问题是BundleTypeAdapter拥有的唯一构造函数是BundleTypeAdapter(Gson Gson),我不太确定如何传入我尚未创建或实例化的对象


有什么想法吗?

对于您描述的案例,Gson有一个解决方案。BundleTypeAdapter在名为factory的静态成员变量中为您定义所需的工厂。要使用它,请致电:

gsonBuilder.registerTypeAdapterFactory(BundleTypeAdapter.FACTORY);
令人惊叹的。谢谢:-)在弄不明白这一点之后,我开始使用hashmap而不是bundle。。。但是如果我现在做的不起作用,我会试试你的建议。