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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何解决这个rec ylerview问题?我的代码中有任何错误吗?在应用程序中唱歌后运行时崩溃相关的知识,希望对你有一定的参考价值。

因为我是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

答案

错误在这里:

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

以上是关于如何解决这个rec ylerview问题?我的代码中有任何错误吗?在应用程序中唱歌后运行时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

cv2.rectangle() 是不是有一个名为“rec”的参数?

在 Oracle 中生成动态 SQL

[深度学习][OCR][原创]CRNN_Chinese_Characters_Rec训练360w数据集提示keyerror错误解决方法

[深度学习][OCR][原创]CRNN_Chinese_Characters_Rec训练360w数据集提示keyerror错误解决方法

解决john不能开多个进程的问题

刷机大师进去rec选择刷机包显示clockworkmod