如何在 putParcelable() 中使用父类型?

问题描述 投票:0回答:0

我有一个实例化

Fragment
并获取类的子对象作为参数的方法。然后,在这种方法中,我想使用
Bundle
将此对象放入
putParcelable()
中,但 Android Studio 不允许我这样做,因为以下错误:

Required type: Parcelable
Provided: Class <capture of ? extends Deportista>

我的方法是:

public static PerfilFragment newInstance(String tipoDeportista, 
                                         Class<? extends Deportista> deportista) {
    PerfilFragment fragment = new PerfilFragment();
    Bundle args = new Bundle();
    args.putString("abiertoComo", tipoDeportista);
    args.putParcelable("deportista", deportista);
    fragment.setArguments(args);
    return fragment;
}

调用

newInstance()
时我不知道
Deportista
的哪个孩子会被发送,我在方法定义中使用了
Class<? extends Deportista>
,但是
putParcelable()
不让我使用这个变量。怎样才能达到我所需要的?

提前谢谢大家!

java android-fragments inheritance bundle parcelable
© www.soinside.com 2019 - 2024. All rights reserved.