gpt4 book ai didi

java - 如何解决此 recylerview 问题?我的代码有什么错误吗?在应用程序中唱歌后在运行时崩溃

转载 作者:行者123 更新时间:2023-11-29 08:25:02 25 4
gpt4 key购买 nike

因为我是 android studio 的新手,对 java 编程或 android studio 没有很好的了解,但不知何故,我只是设法使用运行时 firebase 进行 sign_in 或 sign_up Activity ,并且它成功运行。但是在制作 recyclerView 之后,我的应用程序在每次单击 sign_in 按钮后崩溃时都无法正常工作。

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

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());

这里发生错误...

   //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<DrawerLayout....> 的 ID在 xml 中。 findViewById(R.id.drawer_layout)得到 DrawerLayout并尝试将其转换为 RecyclerView ,这是运行时错误。使用正确的 ID <RecyclerView...>

关于java - 如何解决此 recylerview 问题?我的代码有什么错误吗?在应用程序中唱歌后在运行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53930795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com