回收器视图布局文件引发异常:无法在项目布局中找到 ID 为 ..:id/recyclerView 的视图



我目前正在尝试在Xamarin.Android中实现我的第一个RecyclerView。

在我的主布局文件中,我有一个DrawerLayout,其中包含一个FrameLayout和一个ListView。我试图实现的是,每次单击列表视图中的项目时,将使用片段管理器加载包含回收器视图的片段。

我面临的问题是,每次我从片段中的OnCreateView返回时,我都会得到上述异常。

最让我困惑的是,当我在OnCreateView中逐步执行代码时,FindViewById不会引发异常,并且似乎正确填充了recyclerView。

任何帮助将不胜感激。谢谢。

发现新.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

发现新片段.cs

public class DiscoverNewFragment : Fragment
{
RecyclerView recyclerView;
IngredientsAdapter ingredientsAdapter;
List<Ingredients> listOfIngredients;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.DiscoverNew, container, false);
recyclerView = view.FindViewById(Resource.Id.recyclerView) as RecyclerView;
var layoutMan = new GridLayoutManager(recyclerView.Context, 2, GridLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutMan);
listOfIngredients = IngredientsData.LoadIngredients(this.Activity);
ingredientsAdapter = new IngredientsAdapter(listOfIngredients);
recyclerView.SetAdapter(ingredientsAdapter);
return view;
}
}

主要活动.cs

[Activity(Label = "MyApp", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
. . . 
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
. . .
OnMenuItemClick(0);  // Load Fragment 0 at startup    
}    
. . .    
void OnMenuItemClick(int position)
{         
base.FragmentManager.BeginTransaction().Replace(Resource.Id.frameLayout, new DiscoverNewFragment()).Commit();
this.Title = "Discover";
drawerLayout.CloseDrawer(drawerListView);    
}
}

注意:我省略了MainActivity中的一些代码.cs这与抽屉布局的设置有关

我刚刚通过将以下 nuget 包从版本 27.0.2.1 恢复到版本 26.1.0.1 来解决此问题:- xamarin.android.support.design 我知道这更像是一种解决方法而不是解决方案,但在尝试了不同的事情之后,这是似乎唯一可行的选择。

最新更新