本文介绍了RecyclerView中的setLayoutManager NullPointException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试在RecyclerView中接收带有数组的列表并出现错误:

I'm trying to receive a list with an array in RecyclerView and get error:

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小部件的

RecyclerView widget's

   <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,但这无济于事.

I tried to declare LayoutManager to final, but it doesn't help.

活动代码:

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

        }
    }
}

可能出了什么问题?

推荐答案

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

As @StenSoft noted in comments the problem was insetContentView (R.layout.activity_main_appbar); because of wrong activity layout content, instead ofsetContentView (R.layout.activity_main);

这篇关于RecyclerView中的setLayoutManager NullPointException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:17