得到這個錯誤「IllegalArgumentException:上下文不能爲空。」當我試圖加載圖像使用recylerview和picaso。IllegalArgumentException:上下文不能爲空。而使用picaso&Recyclerview
*我試圖使用recylerview來顯示一堆圖像,我得到了然後我試圖用picaso加載圖像加載。 *我也嘗試使用「這個」關鍵字,但這並沒有奏效。
主要活動。
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);
}
}
我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;
}
}
'Picasso.with(context)'。什麼讓你認爲'context'在這一點上可能不是空的? – njzk2