FirebaseRecyclerAdapter的populateViewHolder()在具有特定值的键被完全移除之前得到调用,从而给出nullpointerexception [英] FirebaseRecyclerAdapter's populateViewHolder() getting called before a key having certain values gets fully removed thus giving nullpointerexception
问题描述
我在应用程序中使用 FirebaseUI
的 FirebaseRecyclerAdapter
来从FirebaseDatabase引用中获取一些数据并显示它在 RecyclerView
中。
以下是我的代码:
private void attachRecyclerViewAdapter(){
方法,并且
Query lastFifty = mDatabase.child(rID).limitToFirst(50);
mRecyclerViewAdapter = new FirebaseRecyclerAdapter< AModelClass,AModelClass.ViewHolder>(
AModelClass.class,R.layout.a_player_layout,APlayersModelClass.ViewHolder.class,lastFifty){
@Override
protected void populateViewHolder(AModelClass.ViewHolder viewHolder,AsModelClass model,int position){
String key = this.getRef(position).getKey();
aReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.getValue ()!= null){
Map< String,String> map =(Map< String,String>)dataSnapshot.getValue();
pA = map.get(pName );
uA = map.get(pUrl);
//在$ b $下面的错误字符串cLatS = map.get(cLat)。trim();
currentLtAU = Double.parseDouble(cLatS);
String cLngS = map.get(cLng)。trim();
currentLnAU = Double.parseDouble(cLngS);
viewHolder.setPName(pA);
viewHolder.setPUrl( UA);
viewHolder.setCurrentLatAU(String.valueOf(currentLtAU));
viewHolder.setCurrentLngAU(String.valueOf(currentLnAU));
} else {
Snackbar snackbar = Snackbar
.make(coordinatorLayout,发生了一些错误,请重试!,Snackbar.LENGTH_SHORT);
snackbar.show();
onBackPressed();
$ b @Override
public void onCancelled(DatabaseError databaseError){
});
}
};
aList.setAdapter(mRecyclerViewAdapter);
$ b $ p
$ b $这里是数据库结构的表示形式:
-app
-requestID
-uniqueKey1
-key:value
-key:value
-cLat: value
-uniqueKey2
-key:value
-key:value
-cLat:value
问题是,只要我从数据库中删除
uniqueKey1
,<$调用c $ c> populateViewHolder()uniqueKey1
下的数据不会被完全删除,因此aReference .child(requestID).child(key)
再次使用uniqueKey1
,但是这次有一些数据,特别是cLat
被从这里删除,从而应用程序崩溃给出java.lang.NullPointerException:尝试调用虚拟方法java.lang.String java.lang.String.trim()'在一个空对象引用
错误的行说明在我的代码中。
我想知道的是如何让
populateViewHolder()
只有在uniqueKey1
及其下的数据被完全删除,然后aReference.child(requestID)时才被调用。 child(key)
只需要uniqueKey2
(其他键)作为参数再次加载数据并显示在recyclerview中?解决方案好吧,我想我想通了一个办法。
代码。
下面是更新的代码:
if(dataSnapshot.getValue )&& dataSnapshot.hasChild(pUrl)&& amp;& dataSnapshot.hasChild(cLat)& amp;& & dataSnapshot.hasChild(cLng)){
Map< String,String> map =(Map< String,String>)dataSnapshot.getValue();
pA = map.get(pName);
uA = map.get(pUrl);
String cLatS = map.get(cLat)。trim();
currentLtAU = Double.parseDouble(cLatS);
String cLngS = map.get(cLng)。trim();
currentLnAU = Double.parseDouble(cLngS);
viewHolder.setPName(pA);
viewHolder.setPUrl(uA);
viewHolder.setCurrentLatAU(String.valueOf(currentLtAU));
viewHolder.setCurrentLngAU(String.valueOf(currentLnAU));
$ b code
$ b现在一切正常,I am using
FirebaseUI
'sFirebaseRecyclerAdapter
in my app to fetch some data from a FirebaseDatabase reference and show it in aRecyclerView
. I'm completing this task successfully.Here's my code:
private void attachRecyclerViewAdapter() { Query lastFifty = mDatabase.child(rID).limitToFirst(50); mRecyclerViewAdapter = new FirebaseRecyclerAdapter<AModelClass, AModelClass.ViewHolder>( AModelClass.class, R.layout.a_player_layout, APlayersModelClass.ViewHolder.class, lastFifty) { @Override protected void populateViewHolder(AModelClass.ViewHolder viewHolder, AsModelClass model, int position) { String key = this.getRef(position).getKey(); aReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.getValue() != null) { Map<String, String> map = (Map<String, String>) dataSnapshot.getValue(); pA = map.get("pName"); uA = map.get("pUrl"); // error on line below String cLatS = map.get("cLat").trim(); currentLtAU = Double.parseDouble(cLatS); String cLngS = map.get("cLng").trim(); currentLnAU = Double.parseDouble(cLngS); viewHolder.setPName(pA); viewHolder.setPUrl(uA); viewHolder.setCurrentLatAU(String.valueOf(currentLtAU)); viewHolder.setCurrentLngAU(String.valueOf(currentLnAU)); } else { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Some error occurred. Please retry!", Snackbar.LENGTH_SHORT); snackbar.show(); onBackPressed(); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } }; aList.setAdapter(mRecyclerViewAdapter); }
Here's database structure's representation:
-app -requestID -uniqueKey1 -key: value -key: value -cLat: value -uniqueKey2 -key: value -key: value -cLat: value
The problem is that as soon as I remove
uniqueKey1
from the database, the code inside thepopulateViewHolder()
method is called and the data underuniqueKey1
don't gets removed completely and thusaReference.child(requestID).child(key)
takes theuniqueKey1
again but this time some of the data, specificallycLat
is removed from here and thus the app crashes givingjava.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
error on the line specified in the code.What I want to know is how can I let the the code inside
populateViewHolder()
called only when theuniqueKey1
along with the data under it is completely removed and then theaReference.child(requestID).child(key)
takes onlyuniqueKey2
(remaining key(s)) as parameter to load data again and show in recyclerview?解决方案Well, I think I figured out a way.
Just placed some conditions in the code.
Here's the updated code:
if (dataSnapshot.getValue() != null) { if (dataSnapshot.hasChild("pName") && dataSnapshot.hasChild("pUrl") && dataSnapshot.hasChild("cLat") && dataSnapshot.hasChild("cLng")) { Map<String, String> map = (Map<String, String>) dataSnapshot.getValue(); pA = map.get("pName"); uA = map.get("pUrl"); String cLatS = map.get("cLat").trim(); currentLtAU = Double.parseDouble(cLatS); String cLngS = map.get("cLng").trim(); currentLnAU = Double.parseDouble(cLngS); viewHolder.setPName(pA); viewHolder.setPUrl(uA); viewHolder.setCurrentLatAU(String.valueOf(currentLtAU)); viewHolder.setCurrentLngAU(String.valueOf(currentLnAU)); } }
and now everything works perfectly fine!
这篇关于FirebaseRecyclerAdapter的populateViewHolder()在具有特定值的键被完全移除之前得到调用,从而给出nullpointerexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!