我正在构建一个从firebase检索数据的应用程序。使用recyclerview显示数据im并且当我移动到活动时从火灾基地获取数据时出现错误。
E/AndroidRuntime: FATAL EXCEPTION: main
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at com.sliit.dailyselfie.ActivityFeed.ActivityFeed.GetDataUpdates(ActivityFeed.java:129)
at com.sliit.dailyselfie.ActivityFeed.ActivityFeed$3.onChildAdded(ActivityFeed.java:156)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.sliit.dailyselfie.ActivityFeed.SharePost] from String value; no single-String constructor/factory method
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
at com.sliit.dailyselfie.ActivityFeed.ActivityFeed.GetDataUpdates(ActivityFeed.java:129)
at com.sliit.dailyselfie.ActivityFeed.ActivityFeed$3.onChildAdded(ActivityFeed.java:156)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
使用的类是 SharePost.java
@JsonIgnoreProperties(ignoreUnknown=true)
public class SharePost {
private String PostSharer;
private String PostType;
private String PostImage;
private String PostDescription;
private String PostedTime;
@JsonIgnore
private String PostID;
public SharePost(){}
public SharePost(String postSharer, String postType, String postImage, String postDescription, String postedTime, String postID) {
PostSharer = postSharer;
PostType = postType;
PostImage = postImage;
PostDescription = postDescription;
PostedTime = postedTime;
PostID = postID;
}
public String getPostSharer() {
return PostSharer;
}
public void setPostSharer(String postSharer) {
PostSharer = postSharer;
}
public String getPostType() {
return PostType;
}
public void setPostType(String postType) {
PostType = postType;
}
public String getPostImage() {
return PostImage;
}
public void setPostImage(String postImage) {
PostImage = postImage;
}
public String getPostDescription() {
return PostDescription;
}
public void setPostDescription(String postDescription) {
PostDescription = postDescription;
}
public String getPostedTime() {
return PostedTime;
}
public void setPostedTime(String postedTime) {
PostedTime = postedTime;
}
}
使用firebase检索编码的类
public class ActivityFeed extends AppCompatActivity {
BottomBar mBottomBar;
ArrayList<SharePost> SharedPosts;
RecyclerView RV;
AdapterAC adapterAC;
Firebase fire;
Dialog d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_feed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Firebase.setAndroidContext(this);
SharedPosts = new ArrayList<>();
fire=new Firebase("https://dailyselfie.firebaseio.com/SharePost");
RV= (RecyclerView)findViewById(R.id.recycler1);
RV.setLayoutManager(new LinearLayoutManager(this));
mBottomBar = BottomBar.attach(this, savedInstanceState);
mBottomBar.noNavBarGoodness();
// this.RefreshData();
mBottomBar.setItemsFromMenu(R.menu.bottomba_menu, new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(@IdRes int menuItemId) {
if (menuItemId == R.id.nav_home) {
Toast.makeText(ActivityFeed.this, "Timeline", Toast.LENGTH_SHORT).show();
} else if (menuItemId == R.id.nav_fav) {
showD();
} else if (menuItemId == R.id.nav_gallery) {
Toast.makeText(ActivityFeed.this, "Favorites", Toast.LENGTH_SHORT).show();
}
mBottomBar.mapColorForTab(0, ContextCompat.getColor(ActivityFeed.this, R.color.bottomPrimary));
mBottomBar.mapColorForTab(1, ContextCompat.getColor(ActivityFeed.this, R.color.bottomPrimary));
mBottomBar.mapColorForTab(2, ContextCompat.getColor(ActivityFeed.this, R.color.bottomPrimary));
}
@Override
public void onMenuTabReSelected(@IdRes int menuItemId) {
}
});
this.RefreshData();
}
@Override
protected void onStart() {
super.onStart();
}
public void RetriveData(){
fire.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
GetDataUpdates(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
GetDataUpdates(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void GetDataUpdates(DataSnapshot dataSnapshot ){
SharedPosts.clear();
for(DataSnapshot ds: dataSnapshot.getChildren()){
SharePost sp = new SharePost();
sp.setPostSharer(ds.getValue(SharePost.class).getPostSharer());
sp.setPostType(ds.getValue(SharePost.class).getPostType());
sp.setPostDescription(ds.getValue(SharePost.class).getPostDescription());
sp.setPostImage(ds.getValue(SharePost.class).getPostImage());
sp.setPostedTime(ds.getValue(SharePost.class).getPostedTime());
SharedPosts.add(sp);
}
if(SharedPosts.size()>0){
adapterAC = new AdapterAC(ActivityFeed.this,SharedPosts);
RV.setAdapter(adapterAC);
}else{
Toast.makeText(ActivityFeed.this, "No data available", Toast.LENGTH_SHORT).show();
}
}
public void RefreshData(){
fire.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
GetDataUpdates(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
GetDataUpdates(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void save(String name,String type,String description,String date){
SharePost sp = new SharePost();
sp.setPostSharer(name);
sp.setPostType(type);
sp.setPostDescription(description);
sp.setPostedTime(date);
fire.push().setValue(sp);
d.dismiss();
}
public void showD(){
d= new Dialog(ActivityFeed.this);
d.setTitle("Save data");
d.setContentView(R.layout.shr);
final EditText name = (EditText)d.findViewById(R.id.editText);
final EditText type = (EditText)d.findViewById(R.id.editText2);
final EditText description = (EditText)d.findViewById(R.id.editText3);
final EditText date = (EditText)d.findViewById(R.id.editText4);
Button b = (Button)d.findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save(name.getText().toString(),type.getText().toString(),description.getText().toString(),date.getText().toString());
name.setText("");
type.setText("");
description.setText("");
date.setText("");
}
});
d.show();
}
}
有人可以解释为什么我会收到此错误以及如何避免错误。
答案 0 :(得分:2)
看起来你正试图从JSON中的某个级别加载太深的对象。
在RetriveData
你有一个儿童听众:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
GetDataUpdates(dataSnapshot);
}
然后在GetDataUpdates
中你有:
public void GetDataUpdates(DataSnapshot dataSnapshot ){
SharedPosts.clear();
for(DataSnapshot ds: dataSnapshot.getChildren()){
SharePost sp = new SharePost();
sp.setPostSharer(ds.getValue(SharePost.class).getPostSharer());
sp.setPostType(ds.getValue(SharePost.class).getPostType());
这最后一个循环现在迭代帖子的属性,而不是帖子。
如果您只是从dataSnapshot
加载帖子,它可能会起作用:
public void GetDataUpdates(DataSnapshot dataSnapshot ){
SharePost sp = dataSnapshot.getValue(SharePost.class);
如果这确实是导致问题的原因,您可能希望使用addValueEventListener()
代替addChildEventListener()
。
答案 1 :(得分:1)
您的SharePost
类似乎在ActivityFeed
内定义。
在这种情况下,你需要将类声明为static,以防止它获得对Jackson不知道如何填充的包含对象的引用:
public static class SharePost {
我不确定这是否是您获得特定异常的原因,但在使用Firebase和嵌套类时这肯定是一个常见的陷阱。