如何在属性中获取具有泛型类的类的值?

时间:2017-01-31 01:24:56

标签: android generics firebase firebase-realtime-database

我有一个派对

public class Party {
    private String password;
    private String host;
    private String nextInLine;
    private String nextUp;
    private Track currentlyPlaying;
    private LinkedList<Guest> guestList;

    public Party() {
        // Default constructor required for calls to DataSnapshot.getValue(Post.class)
    }

    public Party(String password, String hostName) {
        this.password= password;
        this.host= hostName;

        LinkedList<Guest> newGuestList= new LinkedList<>();
        newGuestList.put(hostName, new Guest(hostName));
        this.guestList= newGuestList;
    }

    public Party(String password, String host, String nextInLine, String nextUp, Track
        currentlyPlaying, LinkedList<Guest> guestList) {
        this.password = password;
        this.host = host;
        this.nextInLine = nextInLine;
        this.nextUp = nextUp;
        this.currentlyPlaying= currentlyPlaying;
        this.guestList= guestList;
    }

    ...
}

我正在尝试使用行

从数据库中检索信息
Party party = mutableData.getValue(Party.class);

这会产生ArrayIndexOutOfBounds异常。但是,如果我注释掉

private LinkedList<Guest> guestList;

行,避免错误。我的LinkedList类是

public class LinkedList<Type> extends HashMap<String, Type> {...}

使用默认构造函数,而我的guest类是

public class Guest extends Node {
    private String id;
    private String next;
    private String previous;
    private LinkedList<Track> playlist;

    public Guest(){}

    public Guest(String guestName) {
        super(guestName);
        this.id= guestName;
    }

    ...

}

我尝试过其他提取数据的方法,例如

GenericTypeIndicator<Party> partyType = new
                    GenericTypeIndicator<Party>() {};
Party party = mutableData.getValue(partyType);

Party party= (Party) mutableData.getValue();

无济于事。我相信它与具有泛型类型的LinkedList类有关,但GenericTypeIndicator只有在直接应用于泛型类型时才解决问题,而不是在类的属性中的泛型类型。有谁知道可能会发生什么?

编辑: 堆栈跟踪是

postTransaction:onComplete:DatabaseError: User code called from the Firebase Database runloop threw an exception:
                                                              java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
                                                                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                  at com.google.firebase.database.MutableData.getValue(Unknown Source)
                                                                  at com.app1x.djparty.MainActivity$4.doTransaction(MainActivity.java:293)
                                                                  at com.google.android.gms.internal.zzbml.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbml.zzo(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbml.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbml$8.zzan(Unknown Source)
                                                                  at com.google.firebase.database.connection.idl.zze$2.zzan(Unknown Source)
                                                                  at com.google.firebase.database.connection.idl.zzm$zza.onTransact(Unknown Source)
                                                                  at android.os.Binder.transact(Binder.java:499)
                                                                  at bqq.a(:com.google.android.gms.DynamiteModulesC:82)
                                                                  at bqh.a(:com.google.android.gms.DynamiteModulesC:228)
                                                                  at bop.a(:com.google.android.gms.DynamiteModulesC:966)
                                                                  at bok.a(:com.google.android.gms.DynamiteModulesC:378)
                                                                  at bny.a(:com.google.android.gms.DynamiteModulesC:1139)
                                                                  at bpb.b(:com.google.android.gms.DynamiteModulesC:225)
                                                                  at bpb.a(:com.google.android.gms.DynamiteModulesC:270)
                                                                  at bpi.run(:com.google.android.gms.DynamiteModulesC:1021)
                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                  at java.lang.Thread.run(Thread.java:761)

0 个答案:

没有答案