RecyclerView中的setLayoutManager NullPointException

时间:2015-02-17 20:49:02

标签: android android-5.0-lollipop material-design android-recyclerview

我试图在RecyclerView中接收带有数组的列表并收到错误:

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

RecyclerView小部件

   <android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

我尝试将LayoutManager声明为final,但它没有帮助。

活动代码:

public class MainActivity extends ActionBarActivity {

private RecyclerView recyclerView;
private Toolbar toolbar;
private InfAdapter adapter;


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

    recyclerView = (RecyclerView) findViewById(R.id.drawerList);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    InfAdapter mAdapter = new InfAdapter(this, getData());
    recyclerView.setAdapter(mAdapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

}

适配器代码:

public class InfAdapter extends RecyclerView.Adapter<InfAdapter.MyViewHolder> {

  private final Context context;
    private  List<Information> data = Collections.emptyList();


    public InfAdapter(Context context, List<Information> data){
        this.data = data;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row, parent, false);
        return new MyViewHolder(v);
    }


    @Override
    public void onBindViewHolder(MyViewHolder  holder, int position) {

        Information current  = data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconid);
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {

        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
        icon = (ImageView) itemView.findViewById(R.id.listIcon);

        }
    }
}

可能出了什么问题?

3 个答案:

答案 0 :(得分:23)

正如@StenSoft在评论中指出的那样,问题出在了 setContentView (R.layout.activity_main_appbar);因为活动布局内容错误而不是 setContentView (R.layout.activity_main);

答案 1 :(得分:7)

在我的情况下出现此错误是因为我没有在我的某个活动中创建RecyclerView

检查您的活动布局或片段布局。

答案 2 :(得分:0)

我有类似的东西,但在 kotlin 和里面

RecyclerView.apply {} 

代码,在一行中:

layoutManager = viewManager

最终我发现这是因为我将错误的布局膨胀到片段中:

return inflater.inflate(R.layout.main_fragment, container, false)