Ajit Hogade February 2016

Navigation drawer Recycler View null pointer Exeption

I am working with recycleView as per material design tutorial,I have done all accordingly but my logCat says:

   E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.portal.college.myapplication, PID: 6541
                                                                            java.lang.NullPointerException
                                                                                at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5229)
                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4453)
                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                                                at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                                at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                                                at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                                at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                                at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                                at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                           

Answers


Desdroid February 2016

In your VivzAdapter.java you are returning null instead of your created ViewHolder.

change it to

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.custom_row,parent,false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

then it should work


Viki February 2016

Your VivzAdapter.java is returning null ,Because of what at the run time it throwing out the null pointer exception there.

    package com.portal.college.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by admin on 08-Feb-16.
 */
public class VivzAdapter extends RecyclerView.Adapter<VivzAdapter.MyViewHolder>{
    private final LayoutInflater inflater;
    List<Information> data = Collections.emptyList();

    public VivzAdapter(Context context,List<Information> data){
    inflater=LayoutInflater.from(context);
        this.data=data;
}
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_row,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
Information current = data.get(position);
        holder.title.setText(current.title);
        holder.id.setImageResource(current.iconId);
    }

    @Override
    public int getItemCount() {

            return data.size();

    }
class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;
    ImageView id;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
        id = (ImageView) itemView.findViewById(R.id.listIcon);
    }
}
}

above code might solve the issue.

Post Status

Asked in February 2016
Viewed 2,177 times
Voted 14
Answered 2 times

Search




Leave an answer