尝试使用FirebaseUI RecylerView显示数据时,应用程序崩溃

时间:2018-05-21 02:25:32

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

当我尝试运行此代码时,应用程序崩溃。仅此页面就会导致崩溃。帮我解决这个问题:编译时没有错误,但运行应用程序失败。

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;


import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class AllUsersActivity extends AppCompatActivity {
    private Toolbar mToolBar;
    private RecyclerView allUserList;
    private DatabaseReference allDatabaseUserReference;



    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       // setContentView(.layout.activity_all_users);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setTitle("All Users");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true );


      allUserList = (RecyclerView)findViewById(R.id.all_users_list);
        allUserList.setHasFixedSize(true);
        allUserList.setLayoutManager(new LinearLayoutManager(this));

        allDatabaseUserReference = FirebaseDatabase.getInstance().getReference().child("Users");
        allDatabaseUserReference.keepSynced(true );
     //   mAuth =FirebaseAuth.getInstance();

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<AllUsers,AllUsersViewHolder> firebaseRecyclerAdapter
        = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>
                (
                      AllUsers.class,
                       R.layout.all_users_display_layout,
                          AllUsersViewHolder.class,
                        allDatabaseUserReference

                )
        {
            @Override
            protected void populateViewHolder(AllUsersViewHolder viewHolder, AllUsers model, int position)
            {
               viewHolder.setUser_name(model.getUser_name() );
             //  viewHolder.setUser_name(model.getUser_status());
           //     viewHolder.setUser_image(getApplicationContext(),model.getUser_image());
            }
        };
        allUserList.setAdapter(firebaseRecyclerAdapter);
    }

    public static class AllUsersViewHolder extends RecyclerView.ViewHolder
    {
        View mView;

        private final TextView name;
        public AllUsersViewHolder(View itemView)
         {
            super(itemView );

             mView =itemView;
              name = (TextView) mView.findViewById(R.id.all_users_username);
        }

        public void setUser_name(String user_name)
        {

           // TextView name = (TextView) mView.findViewById(R.id.all_users_username);
            name.setText(user_name);

        }
       public void setUser_status (String user_status)
        {
            TextView status =(TextView) mView.findViewById(R.id.all_user_status);
            status.setText(user_status);
        }

    public void setUser_image(Context ctx,String user_image)
     {
         CircleImageView image =(CircleImageView) mView.findViewById(R.id.all_users_profile_image );
         Picasso.get().load(user_image).into(image);
     }
    }
}

来自FirebaseUI documentation

  

如果您正在显示数据列表,则可能需要将Chat对象绑定到RecyclerView。这意味着实现自定义的RecyclerView.Adapter并使用ChildEventListener协调更新。

     

不用担心,FirebaseUI会自动为您完成所有这些工作!

0 个答案:

没有答案