无法解析(RecyclerView)中的符号findViewById(R.id.xxx)

时间:2018-05-15 10:57:12

标签: android android-recyclerview android-viewholder

我想通过连接到firebase使用RecycleView创建食物菜单,我已将recycler_menu声明为变量,但是当我输入 recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu); 时,显示错误无法解析符号R.id .recycler_menu。需要帮助才能解决此错误。我添加了 implementation 'com.android.support:recyclerview-v7:27.1.1' 支持库,但仍然无法解决此错误。

Home.java

package com.example.liew.idelivery;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.example.liew.idelivery.Common.Common;
import com.example.liew.idelivery.Interface.ItemClickListener;
import com.example.liew.idelivery.Model.Category;
import com.example.liew.idelivery.ViewHolder.MenuViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

public class Home extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Menu");
    setSupportActionBar(toolbar);

    //Init Firebase

    database = FirebaseDatabase.getInstance();
    category = database.getReference("Category");
    TextView txtFullName;


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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

    View headerView = navigationView.getHeaderView(0);
    txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
    txtFullName.setText(Common.currentUser.getName());

    // Load menu

    recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
    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_logout) {

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

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

    }

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

MenuViewHolder.java

package com.example.liew.idelivery.ViewHolder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.liew.idelivery.Interface.ItemClickListener;
import com.example.liew.idelivery.R;

public class MenuViewHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener {

public TextView txtMenuName;
public ImageView imageView;
private ItemClickListener itemClickListener;

public MenuViewHolder(View itemView){
    super(itemView);

    txtMenuName = (TextView)itemView.findViewById(R.id.menu_name);
    imageView = (ImageView)itemView.findViewById(R.id.menu_image);

    itemView.setOnClickListener(this);


}

public void setItemClickListener(ItemClickListener itemClickListener){

    this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View view){
    itemClickListener.onClick(view,getAdapterPosition(),false);
}
}

menu_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="4dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/menu_image"
        android:scaleType="centerCrop"
        android:src="@drawable/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <TextView
        android:id="@+id/menu_name"
        android:text="Name of Menu"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:background="#4f0e0d0e"
        android:textSize="20sp"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</RelativeLayout>

</android.support.v7.widget.CardView>

activity_home.xml

<?xml version="1.0" encoding="utf-8"? 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/background"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home"
    android:background="@color/overlayBackground"
    app:itemTextColor="@android:color/white"
    app:itemIconTint="@android:color/white"
    app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="4dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/menu_image"
        android:scaleType="centerCrop"
        android:src="@drawable/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <TextView
        android:id="@+id/menu_name"
        android:text="Name of Menu"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:background="#4f0e0d0e"
        android:textSize="20sp"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</RelativeLayout>

content_home.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Home"
tools:showIn="@layout/app_bar_home">

<android.support.v7.widget.RecyclerView>
    android:id="@+id/recycler_menu"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

</android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

4 个答案:

答案 0 :(得分:0)

findViewById在xml文件中搜索ID“recycler_menu”。

如果您的xml文件中没有这样的ID,则会出现此错误。

正如我所说,你的XML文件中没有实现这个ID。也许你有更多的xml文件在哪里实现?请显示实现此ID的xml文件

答案 1 :(得分:0)

如果你想要声明

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

你应该像这样创建一个xml文件:

  <?xml version="1.0" encoding="utf-8"?>
   <android.support.v7.widget.RecyclerView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/recycler_menu"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

`

答案 2 :(得分:0)

获取参考的代码完全没问题

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

转到activity_home的XML并重新确认recyclerview的id为名称

  

recycler_menu

像这样

 <?xml version="1.0" encoding="utf-8"?>
   <android.support.v7.widget.RecyclerView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/recycler_menu"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

获取更多帮助,请发送activity_home的XML

答案 3 :(得分:0)

content_home.xml

中试试这个
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_menu"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

您的代码有什么问题,>字符放在错误的位置,原始代码:

<android.support.v7.widget.RecyclerView>
    android:id="@+id/recycler_menu"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

</android.support.v7.widget.RecyclerView>