a getting this error "IllegalArgumentException: Context must not be null."when i am trying to load images using recylerview and picaso.
*I tried to use recylerview to display bunch of images which i was got done then i tried to use picaso to load the image load . *I alos tried to use "this" keyword also but this did not work .
Main Activity.
public class MainActivity extends AppCompatActivity {
//Dummydata or place holderdata.
final static int[] dummyImages={R.drawable.color_black,R.drawable.color_white,R.drawable.color_brown,R.drawable.color_dusty_yellow,R.drawable.color_mustard_yellow,R.drawable.color_red};
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use a linear layout manager
mLayoutManager = new GridLayoutManager(getApplicationContext(),2);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new RecycleAdapter(dummyImages);
mRecyclerView.setAdapter(mAdapter);
}
}
My Adpater
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private String[] adumyDataSet;
private int[] adumyimage;
private Context context;
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public ImageView colorImageView;
// We also create a constructor that accepts the entire item row.
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
colorImageView = (ImageView) itemView.findViewById(R.id.colorsBitch);
}
}
public RecycleAdapter (int[] dummyDataSet){
adumyimage=dummyDataSet;
}
@Override
public RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(RecycleAdapter.ViewHolder holder, int position) {
Picasso.with(context).load(adumyimage[position]).into(holder.colorImageView);
}
@Override
public int getItemCount()
{
return adumyimage.length;
}
}