填充回收站视图,但应用程序崩溃

时间:2018-03-29 07:07:14

标签: android firebaseui

我正在尝试从firebase获取数据并使用recyclerview显示它,但应用程序在几秒钟后崩溃。

    Query q=userRef;
    FirebaseRecyclerOptions options=new FirebaseRecyclerOptions.Builder<post>().setQuery(q,post.class).build();

    firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<post, postviewholder>(options) {

                @Override
                public postviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.allpost,parent,false);
                    return new postviewholder(view);

                }


                @Override
                protected void onBindViewHolder(postviewholder holder, int position, post model) {

                    holder.setFullname(model.getFullname());
                    holder.setDate(model.getDate());
                    holder.setTime(model.getTime());
                    holder.setDescription(model.getDescription());
                    holder.setIndustry(model.getIndustry());


                }

                @Override
                public void onError(DatabaseError error) {
                    super.onError(error);
                    String message=error.getMessage().toString();
                    Toast.makeText(MainActivity.this, "error"+ message, Toast.LENGTH_SHORT).show();
                }
            };

    rList.setAdapter(firebaseRecyclerAdapter);

主要活动布局

main acitvity layout

主要活动

public class MainActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private DatabaseReference userRef,Userref;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private RecyclerView rList;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private Toolbar mtoolbar;
    private String currentuser;
    private FirebaseRecyclerAdapter firebaseRecyclerAdapter;
    private TextView t;
    public static final String TAG="AI";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mAuth=FirebaseAuth.getInstance();
        currentuser=FirebaseAuth.getInstance().getCurrentUser().getUid();
        userRef= FirebaseDatabase.getInstance().getReference().child("Users");
        Userref= userRef.child(currentuser);
        mtoolbar=(Toolbar)findViewById(R.id.toolbar);
        drawerLayout=(DrawerLayout)findViewById(R.id.drawable_layout);
        actionBarDrawerToggle= new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        setSupportActionBar(mtoolbar);
        getSupportActionBar().setTitle("Home");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        rList=(RecyclerView)findViewById(R.id.all_users_post_list);
        LinearLayoutManager layoutManager= new LinearLayoutManager(this);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        rList.setLayoutManager(layoutManager);
        rList.setHasFixedSize(true);




        //navigationView=(NavigationView)findViewById(R.id.nav_view);


          t=(TextView)findViewById(R.id.nav_fullname);`enter code here`

          Userref.addValueEventListener(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 if (dataSnapshot.exists()){

                      String fullname=dataSnapshot.child("fullname").getValue().toString();
                     Toast.makeText(MainActivity.this, "full name "+fullname, Toast.LENGTH_SHORT).show();
                  }
              }

              @Override
              public void onCancelled(DatabaseError databaseError) {

              }
          });
       /** navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                UserMenuSelector(item);
                return false;
            }
        });*/

        Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
        DisplayAlluser();
    }

    private void DisplayAlluser() {

        Query q=userRef;
        FirebaseRecyclerOptions options=new FirebaseRecyclerOptions.Builder<post>().setQuery(q,post.class).build();




        firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<post, postviewholder>(options) {

                    @Override
                    public postviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
                        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.allpost,parent,false);
                        return new postviewholder(view);

                    }


                    @Override
                    protected void onBindViewHolder(postviewholder holder, int position, post model) {

                        holder.setFullname(model.getFullname());
                        holder.setDate(model.getDate());
                        holder.setTime(model.getTime());
                        holder.setDescription(model.getDescription());
                        holder.setIndustry(model.getIndustry());


                    }

                    @Override
                    public void onError(DatabaseError error) {
                        super.onError(error);
                        String message=error.getMessage().toString();
                        Toast.makeText(MainActivity.this, "error"+ message, Toast.LENGTH_SHORT).show();
                    }
                };

        rList.setAdapter(firebaseRecyclerAdapter);

    }


    public static class postviewholder extends RecyclerView.ViewHolder{

        View mView;

        public postviewholder(View itemView) {
            super(itemView);
            mView=itemView;
        }

        public void setFullname(String fullname){
            TextView username=(TextView)mView.findViewById(R.id.post_user_name);
            username.setText(fullname);

        }
        public void setTime(String time){

            TextView user_time=(TextView) mView.findViewById(R.id.time);
            user_time.setText(time);

        }

        public void setDate(String date){
            TextView user_date=(TextView)mView.findViewById(R.id.date);
            user_date.setText(date);
        }
        public void setDescription(String description) {
            TextView user_desc=(TextView)mView.findViewById(R.id.post_description);
            user_desc.setText(description);
        }
        public void setIndustry(String industry){
            TextView user_industry=(TextView)mView.findViewById(R.id.industry);
            user_industry.setText(industry);
        }

        }

    private void UserMenuSelector(MenuItem item) {
        switch (item.getItemId()){
            case R.id.nav_logout:
                mAuth.signOut();
                SendUserToLogin();
                break;
            case R.id.nav_Settings:
                break;

        }



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(actionBarDrawerToggle.onOptionsItemSelected(item)){

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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

        FirebaseUser curentUser=mAuth.getCurrentUser();
        if(curentUser==null){

            SendUserToLogin();
        }
        checkUserExistance();
        firebaseRecyclerAdapter.startListening();





    }

    @Override
    protected void onStop() {
        super.onStop();
        firebaseRecyclerAdapter.stopListening();
    }

    private void checkUserExistance() {

        final String current_user_Id=mAuth.getCurrentUser().getUid();
        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(!dataSnapshot.hasChild(current_user_Id)){
                    Intent i= new Intent(MainActivity.this,setupActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(i);
                    finish();
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void SendUserToLogin() {
        Intent loginIntent=new Intent(MainActivity.this,LoginActivity.class);
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(loginIntent);
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

0 个答案:

没有答案