本文介绍了Android Firebase异常“无法解析类型T"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
他有一个抽象的通用类"ArticleOrdered"和一个子类"DrinkOrdered"(有关类的声明,请参见下面的代码).如果我创建了DrinkOrdered对象并将其保存在Firebase中,则不会有任何问题.当我尝试执行时:DrinkOrdered test = dataSnapshot.getValue(DrinkOrdered.class);
要获取对象,我有
java.lang.IllegalStateException: Could not resolve type T
ArticleOrdered类:
public abstract class ArticleOrdered<T extends Article> {
private int quantity;
private T article;
private String note;
private double price;
public ArticleOrdered(int quantity, T article, String note) {
this.quantity = quantity;
this.article = article;
this.note = note;
this.price = article.getPrice();
}
public ArticleOrdered() {
//FIREBASE CONSTUCTOR
}
public T getArticle() {
return article;
}
public void setArticle(T article) {
this.article = article;
}
...
DrinkOrdered类:
public class DrinkOrdered extends ArticleOrdered<Drink> {
public DrinkOrdered(int quantity, Drink article, String note) {
super(quantity, article, note);
}
public DrinkOrdered() {
//FIREBASE CONSTRUCTOR
}
我还有另一个类"PizzaOrdered"(但是现在不重要了)
TestActivity代码:
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference orderRef = db.getReference("prova");
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DrinkOrdered a = dataSnapshot.getValue(DrinkOrdered.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("PIERF", "onCancelled");
}
};
orderRef.addListenerForSingleValueEvent(listener);
以这种方式声明它们是否正确?对不起,英语不好.
解决方案
我自己使用Jackson编写的以下两行代码来解决了这个问题:
ObjectMapper mapper = new ObjectMapper();
DrinkOrdered a = mapper.convertValue(dataSnapshot.getValue(), DrinkOrdered.class);
hi have an abstract generic class "ArticleOrdered" and a subclass "DrinkOrdered" (see code below for class declaration). If i create a DrinkOrdered object and i save it in Firebase, I have no problems.When i try to execute:
DrinkOrdered test = dataSnapshot.getValue(DrinkOrdered.class);
to get the object, i have
java.lang.IllegalStateException: Could not resolve type T
ArticleOrdered class:
public abstract class ArticleOrdered<T extends Article> {
private int quantity;
private T article;
private String note;
private double price;
public ArticleOrdered(int quantity, T article, String note) {
this.quantity = quantity;
this.article = article;
this.note = note;
this.price = article.getPrice();
}
public ArticleOrdered() {
//FIREBASE CONSTUCTOR
}
public T getArticle() {
return article;
}
public void setArticle(T article) {
this.article = article;
}
...
DrinkOrdered class:
public class DrinkOrdered extends ArticleOrdered<Drink> {
public DrinkOrdered(int quantity, Drink article, String note) {
super(quantity, article, note);
}
public DrinkOrdered() {
//FIREBASE CONSTRUCTOR
}
I have another class "PizzaOrdered" (but it is not important now)
TestActivity code:
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference orderRef = db.getReference("prova");
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DrinkOrdered a = dataSnapshot.getValue(DrinkOrdered.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("PIERF", "onCancelled");
}
};
orderRef.addListenerForSingleValueEvent(listener);
is it correct to declare them in this way? Sorry for bad english.
解决方案
I solved it by myself, using Jackson with these 2 lines of code:
ObjectMapper mapper = new ObjectMapper();
DrinkOrdered a = mapper.convertValue(dataSnapshot.getValue(), DrinkOrdered.class);
这篇关于Android Firebase异常“无法解析类型T"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!