如何解决这个rec​​ylerview问题?我的代码有什么错误吗?在应用中唱歌后在运行时崩溃

时间:2018-12-26 10:31:24

标签: java android android-studio android-recyclerview

由于我是android studio的新手,并且对Java编程或android studio没有足够的了解,但是以某种方式我只是设法通过运行时Firebase进行sign_in或sign_up活动,并且成功运行。但是制作完recyclerView之后,我的应用程序每次在单击sign_in按钮后崩溃都无法正常运行。

我尝试更新并使用与recyclerview兼容的appcompat-v7:28的所有版本,但是它对我不起作用。这给我一个错误

  

原因:java.lang.ClassCastException:android.support.v4.widget.DrawerLayout无法转换为android.support.v7.widget.RecyclerView           在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());

这里发生错误...

   //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

1 个答案:

答案 0 :(得分:5)

错误在这里:

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

R.id.drawer_layout是xml中<DrawerLayout....>的ID。 findViewById(R.id.drawer_layout)得到DrawerLayout并尝试将其强制转换为RecyclerView,即RuntimeError。使用正确的<RecyclerView...>

ID