Display a RecyclerView in Fragment

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I'm trying out the new RecyclerView in Android Lollipop and I'm stuck.

I'm trying to receive a list, with an icon and a TextView to the right of the icon, inside a Fragment.

I found this great tutorial on how to set up a RecyclerView. I have followed every point and only changed the item_layout.xml to fit my needs.

The project builds without any errors but when it launches on my device I'm getting this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fredrikaldgard.materialcolors/com.fredrikaldgard.materialcolors.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

I've tried to google the problem but I'm quite an amateur with Android development.

Here's my MainActivity:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      // 1. get a reference to recyclerView     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);      // 2. set layoutManger     recyclerView.setLayoutManager(new LinearLayoutManager(this));      // this is data fro recycler view     ItemData itemsData[] = { new ItemData("Indigo",R.drawable.circle),             new ItemData("Red",R.drawable.color_ic_launcher),             new ItemData("Blue",R.drawable.indigo),             new ItemData("Green",R.drawable.circle),             new ItemData("Amber",R.drawable.color_ic_launcher),             new ItemData("Deep Orange",R.drawable.indigo)};       // 3. create an adapter     MyAdapter mAdapter = new MyAdapter(itemsData);     // 4. set adapter     recyclerView.setAdapter(mAdapter);     // 5. set item animator to DefaultAnimator     recyclerView.setItemAnimator(new DefaultItemAnimator());

And my MyAdapter:

public class MyAdapter extends RecyclerView.Adapter { private ItemData[] itemsData;  public MyAdapter(ItemData[] itemsData) {     this.itemsData = itemsData; }  // Create new views (invoked by the layout manager) @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,                                                int viewType) {     // create a new view     View itemLayoutView = LayoutInflater.from(parent.getContext())             .inflate(R.layout.item_layout, null);      // create ViewHolder      ViewHolder viewHolder = new ViewHolder(itemLayoutView);     return viewHolder; }  // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder viewHolder, int position) {      // - get data from your itemsData at this position     // - replace the contents of the view with that itemsData      viewHolder.txtViewTitle.setText(itemsData[position].getTitle());     viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());   }  // inner class to hold a reference to each item of RecyclerView public static class ViewHolder extends RecyclerView.ViewHolder {      public TextView txtViewTitle;     public ImageView imgViewIcon;      public ViewHolder(View itemLayoutView) {         super(itemLayoutView);         txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);         imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!