Android Recycler视图OnbindViewHolder抛出空对象引用异常

时间:2016-03-07 20:45:37

标签: android nullpointerexception android-recyclerview

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Person> persons;

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

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    initializeData();
    mAdapter = new MyAdapter(persons);
    mRecyclerView.setAdapter(mAdapter);
}

public class Person
{
    String name;
    String age;
    int photoId;
    Person(String name, String age, int photoId)
    {
        this.name = name;
        this.age = age;
        this.photoId = photoId;
    }
}

private void initializeData()//initializing the person class
{
    persons = new ArrayList<>();
    persons.add(new Person("Emma Wilson", "23 years old",      R.drawable.ic_action_name));
    persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_action_name2));
    persons.add(new Person("Lillie Watts", "35 years old", R.drawable.logo));
}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Person> mDataset;

    public class ViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        public ViewHolder(View v) {
            super(v);
            cv  = (CardView)findViewById(R.id.cv);
            personName = (TextView)findViewById(R.id.person_name);
            personAge = (TextView)findViewById(R.id.person_age);
            personPhoto = (ImageView)findViewById(R.id.person_photo);
        }
    }

    public MyAdapter(List<Person> myDataset) {
        this.mDataset = myDataset;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_example, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

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

        Person p = mDataset.get(position);

        //here i get an exception indicating holder is null exception

        holder.personName.setText(p.name);
        holder.personAge.setText(p.age);
        holder.personPhoto.setImageResource(p.photoId);
    }

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

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

}

尝试使用cardview实现基本的回收站视图。 Person类为将使用卡的人保存数据并显示在recycleler视图中,但在onBindViewHolder中它会抛出零点异常。

03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio   E/AndroidRuntime: FATAL EXCEPTION: main
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime: Process: com.wolf.vivekverma.vedio, PID: 1518
 03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at com.wolf.vivekverma.vedio.MainActivity$MyAdapter.onBindViewHolder(MainActivity.java:97)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at com.wolf.vivekverma.vedio.MainActivity$MyAdapter.onBindViewHolder(MainActivity.java:61)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at    android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:550)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5312) 
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:     at      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
-08 01:59:28.771 1518-1518/com.wolf.vivekverma.vedio E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.j

0 个答案:

没有答案