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

问题描述 投票:2回答:1

因为我是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无法在com.example.shoplaptop.gujjardairyfarmapp.Home.onCreate(Home.java:79)中强制转换为android.support.v7.widget.RecyclerView

    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

java android android-studio android-recyclerview
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.