2016-09-22 53 views
-1

得到這個錯誤「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; 
} 

}

+0

'Picasso.with(context)'。什麼讓你認爲'context'在這一點上可能不是空的? – njzk2

回答

3

使用此: 發送上下文適配器作爲構造

public RecycleAdapter (int[] dummyDataSet,Context context){ 
    this.context=context; 
    adumyimage=dummyDataSet; 

} 

在您的活性狀況ty

mAdapter = new RecycleAdapter(dummyImages,this); 
+0

您無法在適配器中調用getActivity()。它可以在片段中調用。並且op正在使用AppCompatActivity –

+0

您是對的。我編輯了我的答案。它可以工作嗎? –

+0

喲.. upvoted :) –

相關問題