简体   繁体   中英

how to fix this recylerview issue? is there any error in my code ? on runtime after singing in the app get crash

As I am new to android studio and don't have a good knowledge about java programming or android studio, but somehow I just managed to make sign_in or sign_up activity with run time firebase and it runs successfully. But after making recyclerView, my app doesn't work properly every time it gets crash after clicking in sign_in Button.

I try to update and use all the version of recyclerview which is compatible with appcompat-v7:28 but it doesn't work for me. It gives me an error

Caused by: java.lang.ClassCastException: android.support.v4.widget.DrawerLayout cannot be cast to android.support.v7.widget.RecyclerView at com.example.shoplaptop.gujjardairyfarmapp.Home.onCreate(Home.java:79)

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //set Name for USer according to firebase
    View headerView = navigationView.getHeaderView(0);
    txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
    txtFullName.setText(common.currentUser.getName());

error occurs here...

   //LoadMenu
    recycler_menu = (RecyclerView)findViewById(R.id.drawer_layout);
    recycler_menu.setHasFixedSize(true);
    layoutManager =new LinearLayoutManager(this);
    recycler_menu.setLayoutManager(layoutManager);

    loadMenu();
}

private void loadMenu() {
    FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category) {
        @Override
        protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) {
            viewHolder.txtMenuName.setText(model.getName());
            Picasso.with(getBaseContext()).load(model.getImage())
                    .into(viewHolder.imageView);
            final Category clickitem = model;
            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    Toast.makeText(Home.this, ""+clickitem.getName(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    };

    recycler_menu.setAdapter(adapter);
}


@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_menu) {
        // Handle the camera action
    } else if (id == R.id.nav_Cart) {

    } else if (id == R.id.nav_orders) {

    } else if (id == R.id.nav_log_out) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

dependencies

Error here:

recycler_menu = (RecyclerView)findViewById(R.id.drawer_layout);

R.id.drawer_layout is ID of <DrawerLayout....> in xml. findViewById(R.id.drawer_layout) gets DrawerLayout and try to cast it to RecyclerView , which is RuntimeError. Use correct id of <RecyclerView...>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM