【发布时间】:2016-05-05 18:40:10
【问题描述】:
我正在尝试向我的应用程序添加用户身份验证,但我不明白为什么我无法通过 FirebaseRecyclerAdapter 中的以下 ref 访问特定用户。
waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/";
final Firebase userRef = new Firebase(waypointsUrl);
FATAL EXCEPTION: main Process: com.example.rasmusjosefsson.rjcar, PID: 7797 com.firebase.client.FirebaseException: Failed to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Ha
我的 JSON 结构。 还想知道是否可以使用“添加日期:2016-05-05 03:50:02”:作为关键?
{
"users" : {
"0057242b-81e2-4f97-bca7-b671212614ba" : {
"email" : "kalle@hotmail.se",
"waypoints" : {
"Date added: 2016-05-05 03:50:02" : {
"-KH-eHHxVpf--IcnYgIq" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eK6z9vIefgcI5Pq2" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eMIl8zB_Yq67SY69" : {
"latitude" : 123,
"longitude" : 123
}
}
}
},
"1a96a633-7e67-41b8-9aa7-c70d4b7eb59c" : {
"email" : "josefssons@hotmail.se",
"waypoints" : {
"Date added: 2016-05-05 03:46:21" : {
"-KH-bcrZwOR2c3Ouc-hv" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-bhdzAatR84pcX1_h" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-dz0jw_UTsG2ojFJM" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-e0qLa2fY1fsFpiTS" : {
"latitude" : 123,
"longitude" : 123
}
},
"Date added: 2016-05-05 03:51:09" : {
"-KH-eXXo5OGSG7GIisMf" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eZPi1spUbFUwkmfh" : {
"latitude" : 123,
"longitude" : 123
},
"-KH-eZSt613mKv1cJyox" : {
"latitude" : 123,
"longitude" : 123
}
},
我猜这里的某个地方是错误的,我没有看到。
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyLatLngBack2 {
@JsonProperty("email")
private String email;
@JsonProperty("waypoints")
private TreeMap<String, Waypoints> waypoints;
public MyLatLngBack2(String email, TreeMap<String, WaypointsBack2> waypoints) {
this.email = email;
this.waypoints = waypoints;
}
public MyLatLngBack2() {}
public String getEmail() {
return email;
}
public TreeMap<String, WaypointsBack2> getWaypoints() {
return waypoints;
}
public void setWaypoints(TreeMap<String, WaypointsBack2> waypoints) {
this.waypoints = waypoints;
}
}
航点数据
@JsonIgnoreProperties(ignoreUnknown = true)
public class WaypointsBack2 {
@JsonProperty("longitude")
private double longitude;
@JsonProperty("latitude")
private double latitude;
public WaypointsBack2(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public WaypointsBack2() {
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
}
下面是剩下的代码
mRef = new Firebase(Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}
try {
mUserId = mRef.getAuth().getUid();
} catch (Exception e) {
loadLoginView();
}
mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(false);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setReverseLayout(false);
mRecyclerView.setLayoutManager(manager);
// https://todoapprj.firebaseio.com/users/1a96a633-7e67-41b8-9aa7-c70d4b7eb59c
waypointsUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/";
final Firebase userRef = new Firebase(waypointsUrl);
// (LatLngViewHolder.class, userRef) gives me bounce to blaha
mRecycleViewAdapter = new FirebaseRecyclerAdapter<MyLatLngBack2, LatLngViewHolder>(MyLatLngBack2.class, R.layout.list_item, LatLngViewHolder.class, mRef) {
@Override
public void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLngBack2 myLatLng, int position) {
// gets me the root email (wrong one)
String email = myLatLng.getEmail();
}
};
mRecyclerView.setAdapter(mRecycleViewAdapter);
}
【问题讨论】:
-
如果您在堆栈跟踪中向下滚动,将会有更准确的错误描述。那会很有帮助。
-
抱歉,刚刚更新。我不知道该怎么办,一直在尝试一切……但还不是正确的。
-
您将错误的引用传递给
new FirebaseRecyclerAdapter<MyLatLngBack2, LatLngViewHolder>(MyLatLngBack2.class, R.layout.list_item, LatLngViewHolder.class, mRef)。最后一个参数应该是userRef。 -
我想我不清楚当我传入 userRef 时,我得到了 Bounce 堆栈跟踪,如果我通过 mRef,我至少可以到达用户 root。但不会因为反弹而进一步。
-
当我从
MyLatLangBack2POJO 中删除waypoints时:public MyLatLngBack2(String email, HashMap<String, WaypointsBack2> waypoints) { this.email = email; this.waypoints = waypoints; }然后它将停止失败反弹异常,那么我应该如何真正构建我的 POJO?
标签: android firebase firebase-realtime-database firebase-authentication