FirebaseRecyclerAdapter的populateViewHolder()在具有特定值的键被完全移除之前得到调用,从而给出nullpointerexception [英] FirebaseRecyclerAdapter's populateViewHolder() getting called before a key having certain values gets fully removed thus giving nullpointerexception

查看:140
本文介绍了FirebaseRecyclerAdapter的populateViewHolder()在具有特定值的键被完全移除之前得到调用,从而给出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's FirebaseRecyclerAdapter in my app to fetch some data from a FirebaseDatabase reference and show it in a RecyclerView. 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 the populateViewHolder() method is called and the data under uniqueKey1 don't gets removed completely and thus aReference.child(requestID).child(key) takes the uniqueKey1 again but this time some of the data, specifically cLat is removed from here and thus the app crashes giving java.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 the uniqueKey1 along with the data under it is completely removed and then the aReference.child(requestID).child(key) takes only uniqueKey2 (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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆