Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

java - android firebase unable to instantiate abstract class when call getValue() in listener

I have a outfit class that has an abstract class as one of its attributes. When I listen for changes in the database I get this error java.lang.RuntimeException: java.lang.InstantiationException: Can't instantiate abstract class com.example.recouture.Item

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Outfits");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                outfits.add(singleSnapshot.getValue(Outfit.class)); //error is here
            }

Is it possible to circumvent this because making Item non - abstract would cause design problems in my other parts of code. Item is abstract because it can contain Shirt,Pants,etc which extend from Item class.

Outfit.java

public class Outfit implements Parcelable{

private List<Item> itemList = new ArrayList<>();
private String mName;
private String mImageUrl;


public Outfit() {}


public Outfit(String mName, String mImageUrl,List<Item> items) {
    this.mName = mName;
    this.mImageUrl = mImageUrl;
    this.itemList = items;
}

public List<Item> getItemList() {
    return itemList;
}

protected Outfit(Parcel in) {
    in.readList(itemList,Item.class.getClassLoader());
    mName = in.readString();
    mImageUrl = in.readString();
}

public static final Creator<Outfit> CREATOR = new Creator<Outfit>() {
    @Override
    public Outfit createFromParcel(Parcel in) {
        return new Outfit(in);
    }

    @Override
    public Outfit[] newArray(int size) {
        return new Outfit[size];
    }
};

public String getmName() {
    return mName;
}

public String getmImageUrl() {
    return mImageUrl;
}

@Override
public String toString() {
    return mName;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeList(itemList);
    parcel.writeString(mName);
    parcel.writeString(mImageUrl);
}

public void setItemList(List<Item> itemList) {
    this.itemList = itemList;
}

}

public abstract class Item implements Parcelable {

private String mName;

private String mColor;

private String mImageUrl;

private List<TagHolder> tags = new ArrayList<>();

private String mKey;




public Item(){

}



public Item(String mName, String mColor, String mImageUrl) {
    this.mName = mName;
    this.mColor = mColor;
    this.mImageUrl = mImageUrl;
}


protected Item(Parcel in) {
    mName = in.readString();
    mColor = in.readString();
    mImageUrl = in.readString();
    tags = in.createTypedArrayList(TagHolder.CREATOR);
    mKey = in.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mName);
    dest.writeString(mColor);
    dest.writeString(mImageUrl);
    dest.writeTypedList(tags);
    dest.writeString(mKey);
}

@Override
public int describeContents() {
    return 0;
}


public void setmName(String mName) {
    this.mName = mName;
}

public void setmImageUrl(String mImageUrl) {
    this.mImageUrl = mImageUrl;
}

public List<TagHolder> getTags() {
    return tags;
}

public String getmColor() {
    return mColor;
}

public String getmImageUrl() {
    return mImageUrl;
}

public void setmColor(String mColor) {
    this.mColor = mColor;
}

public String getmName() {
    return mName;
}

public void setTags(List<TagHolder> tags) {
    this.tags = tags;
}

@Exclude // dont need this in our firebase database
public String getKey() {
    return mKey;
}

@Exclude
public void setMkey(String key) {
    mKey = key;
}


public abstract String getCategory();

}

firebase database structure:

userid : {
    outfits : {
        uniqueKey1 : {
            itemList : {
                0: {..}
                1: {...}
            }
                mName : {...}
                mImageUrl : {...}

         } // uniquekey1
         uniqueKey2 : {
            itemList : {
                0: {..}
                1: {...}
            }
                mName : {...}
                mImageUrl : {...}
            } // end unique key 2 
         }
     } //end outfits
} // end user id 

full stack trace :

08-02 00:04:28.191 6353-6353/com.example.recouture E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.recouture, PID: 6353
java.lang.RuntimeException: java.lang.InstantiationException: Can't instantiate abstract class com.example.recouture.Item
    at com.google.android.gms.internal.zzelx.zze(Unknown Source:305)
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source:196)
    at com.google.android.gms.internal.zzelw.zza(Unknown Source:301)
    at com.google.android.gms.internal.zzelw.zza(Unknown Source:60)
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source:0)
    at com.google.android.gms.internal.zzelx.zze(Unknown Source:125)
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source:196)
    at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
    at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
    at com.example.recouture.Outfit.ViewOutfits$2.onDataChange(ViewOutfits.java:74) // line of my error 
    at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
    at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
    at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
    at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.InstantiationException: Can't instantiate abstract class com.example.recouture.Item
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at com.google.android.gms.internal.zzelx.zze(Unknown Source:53)
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source:196)?
    at com.google.android.gms.internal.zzelw.zza(Unknown Source:301)?
    at com.google.android.gms.internal.zzelw.zza(Unknown Source:60)?
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source:0)?
    at com.google.android.gms.internal.zzelx.zze(Unknown Source:125)?
    at com.google.android.gms.internal.zzelw.zzb(Unknown Source:196)?
    at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)?
    at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)?
    at com.example.recouture.Outfit.ViewOutfits$2.onDataChange(ViewOutfits.java:74)?
    at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)?
    at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)?
    at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)?
    at com.google.android.gms.internal.zzeig.run(Unknown Source:71)?
    at android.os.Handler.handleCallback(Handler.java:873)?
    at android.os.Handler.dispatchMessage(Handler.java:99)?
    at android.os.Looper.loop(Looper.java:193)?
    at android.app.ActivityThread.main(ActivityThread.java:6669)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...