我正在使用FirebaseRecyclerAdapter从Firebase检索用户数据 使用onBindViewHolder和onCreateViewHolder。我的问题是,每当我将FirebaseRecyclerViewAdapter设置为开始监听时,它会使我的FindFriendsActivity崩溃。 onStart方法中的这一行代码是导致问题“ adapter.startListening()”的那一行。我该怎么办
这是“查找朋友”活动
public class FindFriendsActivity extends AppCompatActivity {
private RecyclerView findFriendsRecyClerList;
private DatabaseReference usersRef;
private FirebaseRecyclerAdapter<Contacts, findFriendsViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_find_friends );
usersRef = FirebaseDatabase.getInstance ().getReference ().child ( "Users" );
findFriendsRecyClerList = (RecyclerView) findViewById ( R.id.find_friends_recyclerview );
findFriendsRecyClerList.setLayoutManager ( new LinearLayoutManager ( this ) );
android.support.v7.widget.Toolbar minds = findViewById ( R.id.find_friends_toolbar );
setSupportActionBar ( minds );
getSupportActionBar ().setDisplayShowHomeEnabled ( true );
getSupportActionBar ().setTitle ( "FindFriends" );
FirebaseRecyclerOptions<Contacts> options = new FirebaseRecyclerOptions.Builder<Contacts> ().setQuery ( usersRef,Contacts.class ).build ();
adapter = new FirebaseRecyclerAdapter <Contacts, findFriendsViewHolder> (options) {
@Override
protected void onBindViewHolder(@NonNull findFriendsViewHolder holder , int position , @NonNull Contacts model) {
holder.username.setText ( model.getName () );
holder.usertown.setText ( model.getTownname () );
Picasso.get ().load ( model.getImage () ).into ( holder.profileimage );
}
@NonNull
@Override
public findFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup , int i) {
View view = LayoutInflater.from ( viewGroup.getContext () ).inflate ( R.layout.users_display_layout, viewGroup, false );
findFriendsViewHolder viewHolder = new findFriendsViewHolder ( view );
return viewHolder;
}
};
findFriendsRecyClerList.setAdapter ( adapter );
}
@Override
protected void onStart() {
super.onStart ();
adapter.startListening ();
}
@Override
public void onStop(){
super.onStop ();
adapter.stopListening ();
}
public static class findFriendsViewHolder extends RecyclerView.ViewHolder{
TextView username, usertown;
CircleImageView profileimage;
public findFriendsViewHolder(@NonNull View itemView) {
super ( itemView );
username = itemView.findViewById ( R.id.user_profile_name );
usertown = itemView.findViewById ( R.id.user_town_name );
profileimage = itemView.findViewById ( R.id.users_profile_image );
}
}
}