Reputation: 154
Can some one assist me to know what has been causing this error in My recyclerview i have tried changing the variable name at
Adapter1.java:48
But it still giving me the same error.
java.lang.NullPointerException
at ke.co.clickaway.squirrel.clickawaytechnologies.adpter.Adapter1.getItemCount(Adapter1.java:48)
the code is as follows
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ArrayList<DataList> list;
//in the onCreate Bundle
RecyclerView rview=(RecyclerView) findViewById(R.id.rview);
Adapter1 adapter1=new Adapter1(this,list);
rview.setAdapter(adapter1);
StaggeredGridLayoutManager lm=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
rview.setLayoutManager(lm);
the information class
public class DataList {
public int imageView;
public String title;
public static List<DataList> getData(){
ArrayList<DataList> mList=new ArrayList<>();
int[] imag={R.drawable.web_ic,R.drawable.palette_ic,R.drawable.pencil,R.drawable.sware_ic,R.drawable.phone_ic};
String[] lable={"Web Development","Branding","Graphic Design","Systems","Mobile Apps"};
for(int i=0;i<lable.length&& i<imag.length;i++){
DataList dataList=new DataList();
dataList.title=lable[i];
dataList.imageView=imag[i];
mList.add(dataList);
}
return mList;
}
}
And My adapter
public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> {
private List<DataList> mList;
private Context mContext;
public Adapter1(Context context,List<DataList> list){
mList=list;
mContext=context;
}
private Context getContext(){
return mContext;
}
@Override
public Adapter1.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context=parent.getContext();
LayoutInflater inflater=LayoutInflater.from(context);
View customView=inflater.inflate(R.layout.custom_view,parent,false);
ViewHolder viewHolder=new ViewHolder(customView);
return viewHolder;
}
@Override
public void onBindViewHolder(Adapter1.ViewHolder holder, int position) {
DataList dataList=mList.get(position);
holder.imag.setImageResource(dataList.imageView);
holder.lable.setText(dataList.title);
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imag;
public TextView lable;
public ViewHolder(View itemView) {
super(itemView);
imag= (ImageView)itemView.findViewById(R.id.imageView1);
lable=(TextView)itemView.findViewById(R.id.textView1);
}
}
}
my custom View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="@drawable/one" />
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="dummy Text" />
</LinearLayout>
the RecyclerView snippet in activty_main.xml
<android.support.v7.widget.RecyclerView
android:layout_below="@id/appbarlayout"
android:id="@+id/rview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Upvotes: 2
Views: 565
Reputation: 1031
list is null. So initialize your list before you create your adapter or change getItemCount() method in your adapter to this:
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
Upvotes: 2
Reputation: 615
As per your code the list is null, so instead of using ArrayList<DataList> list
in MainActivity use ArrayList<DataList> list = new ArrayList<>()
or ArrayList<DataList> list = DataList.getData()
Upvotes: 1
Reputation: 5821
try add this code :
list = Datalist.getData();
before this :
Adapter1 adapter1=new Adapter1(this,list);
Upvotes: 0