RecyclerView显示和NullpointerException问题

时间:2018-09-23 10:45:23

标签: android android-recyclerview nullpointerexception

我正面临NullPointerException,并且我的应用程序崩溃了,如果我放置user.getImageURL!= null则不显示图像

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        User user=mUser.get(position);
    holder.username.setText(user.getUsername());
   if(user.getImageURL().equals("default"))
    {
        holder.pic.setImageResource(R.mipmap.ic_launcher);

    }
    else
    {
        Glide.with(mContext).load(user.getImageURL()).into(holder.pic);
    }


}

我正面临NullPointerException和RecycleViewer并发症

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    mUser.clear();
    for(DataSnapshot snapshot:dataSnapshot.getChildren())
    {

        User user=snapshot.getValue(User.class);
            assert user !=null;
            assert firebaseUser !=null;
       if(!user.getId().equals(firebaseUser.getUid()))
        {
            mUser.add(user);
        }
    }


    userAdapter=new UserAdapter(getContext(),mUser);
    recyclerView.setAdapter(userAdapter);
}

如果可以进行任何更改,请指导我。 是的,我也删除了.setHasFixedSize(true),但没有用。

这是我的代码:-> UserModel:

    public class User {
    private String id;
    private String username;
    private String imageURL;

    public User(String id, String username, String imageURL) {
        this.id = id;
        this.username = username;
        this.imageURL = imageURL;
    }

    public User() {

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }
}

UserAdapter:

    import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.sannith.androidchats.MessageActivity;
import com.example.sannith.androidchats.Model.User;
import com.example.sannith.androidchats.R;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    private Context mContext;
    private List<User> mUser;

    public UserAdapter(Context mContext,List<User> mUser)
    {
        this.mUser=mUser;
        this.mContext=mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(mContext).inflate(R.layout.user_item,parent,false);
        return new UserAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            User user=mUser.get(position);
        holder.username.setText(user.getUsername());
       if(user.getImageURL().equals("default"))
        {
            holder.pic.setImageResource(R.mipmap.ic_launcher);

        }
        else
        {
            Glide.with(mContext).load(user.getImageURL()).into(holder.pic);
        }


    }

    @Override
    public int getItemCount() {
        return mUser.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView username;
        public ImageView pic;

        public ViewHolder(View itemView)
        {
            super(itemView);

            username=(TextView) itemView.findViewById(R.id.username2);
            pic=(ImageView) itemView.findViewById(R.id.pic2);
        }
    }
}

UserFragment:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.example.sannith.androidchats.Adapter.UserAdapter;
import com.example.sannith.androidchats.Model.User;
import com.example.sannith.androidchats.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;


public class UsersFragment extends Fragment {
        private UserAdapter userAdapter;


        private RecyclerView recyclerView;
        private List<User> mUser;

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_users,container,false);

        recyclerView=view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mUser=new ArrayList<>();

        readUsers();

        return view;
    }

    private void readUsers()
    {
        final FirebaseUser firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Users");

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUser.clear();
                for(DataSnapshot snapshot:dataSnapshot.getChildren())
                {

                    User user=snapshot.getValue(User.class);
                        assert user !=null;
                        assert firebaseUser !=null;
                   if(!user.getId().equals(firebaseUser.getUid()))
                    {
                        mUser.add(user);
                    }
                }


                userAdapter=new UserAdapter(getContext(),mUser);
                recyclerView.setAdapter(userAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

也许User对象的imageUrl可以为null。因此,在将它与某些对象进行比较之前,您需要检查它是否不为NULL:

String url = user.getImageURL();
if (url != null && url.equals("default")) {
    holder.pic.setImageResource(R.mipmap.ic_launcher);
}