【问题标题】:Cannot resolve symbol in (RecyclerView)findViewById(R.id.xxx)无法解析 (RecyclerView)findViewById(R.id.xxx) 中的符号
【发布时间】:2018-05-15 10:57:11
【问题描述】:

我想通过连接到 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>

【问题讨论】:

  • 对于那些愿意帮助我但不太了解我的代码的人,可以给我你的电子邮件地址。我会将我的完整代码通过电子邮件发送给你以摆脱这个错误。
  • 也添加activity_home xml代码
  • 发布您的 activity_home.xml
  • 添加了activity_home.xml
  • 我发现了我的错误。它位于 content_home.xml 中。但是我应该如何修改它以便没有错误?

标签: android android-recyclerview android-viewholder


【解决方案1】:

在您的 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>

你的代码有问题是&gt;字符放错了位置,你原来的代码:

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

【讨论】:

  • 感谢老兄的帮助。我没有意识到我在 content_home.xml 中缺少 >。太搞笑了。
  • @LiewSyetChau 请善意公平地将这篇文章标记为答案。谭会因为他的帮助而获得声誉。
【解决方案2】:

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

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

正如我所见,此 ID 未在您的 XML 文件中实现。也许你有更多的 xml-Files 在哪里实现它?请显示实现此 ID 的 xml 文件

【讨论】:

  • 我发现了我的错误。它位于 content_home.xml 中。但是我应该如何修改它以便没有错误?
  • 在布局文件中发现意外的文本:"android:id="@+id/recycler_menu" android:scrollbars="vertical" android:layout_width="..." less... (Ctrl+ F1) 布局资源文件应该只包含元素和属性。在文件中发现的任何 XML 文本内容都可能是偶然的(如果文本类似于 XML 并且开发人员认为该文本是功能性的,则可能是危险的)
【解决方案3】:

如果你想认领

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

`

【讨论】:

  • 在布局文件中发现意外的文本:"android:id="@+id/recycler_menu" android:scrollbars="vertical" android:layout_width="..." less... (Ctrl+ F1) 布局资源文件应该只包含元素和属性。在文件中发现的任何 XML 文本内容都可能是偶然的(如果文本类似于 XML 并且开发人员认为该文本是功能性的,则可能是危险的)
  • 我发现了我的错误。它位于 content_home.xml 中。但是我应该如何修改它以便没有错误?
【解决方案4】:

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

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

转到activity_home的XML并重新确认recyclerview有id with name

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

【讨论】:

  • 在布局文件中发现意外的文本:"android:id="@+id/recycler_menu" android:scrollbars="vertical" android:layout_width="..." less... (Ctrl+ F1) 布局资源文件应该只包含元素和属性。在文件中发现的任何 XML 文本内容都可能是偶然的(如果文本类似于 XML 并且开发人员认为该文本是功能性的,则可能是危险的)
  • 我发现了我的错误。它位于 content_home.xml 中。但是我应该如何修改它以便没有错误?
猜你喜欢
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
相关资源
最近更新 更多