2014-11-06 68 views
9

我試圖通過在我的項目上使用Picasso庫來設置圖像。Picasso IllegalArgumentException目標不能爲空

當我單擊視圖的圖像時,出現畢加索執行錯誤。

logcat的應用

java.lang.IllegalArgumentException: Target must not be null. 
      at com.squareup.picasso.RequestCreator.into(RequestCreator.java:340) 
      at com.squareup.picasso.RequestCreator.into(RequestCreator.java:326) 
      at com.zafer.celaloglu.FragmentsandActivities.UnfoldableDetailsFragment.openDetails(UnfoldableDetailsFragment.java:89) 
      at com.zafer.celaloglu.model.PaintingsAdapter.onClick(PaintingsAdapter.java:52) 
      at android.view.View.performClick(View.java:4084) 
      at android.view.View$PerformClick.run(View.java:16966) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4745) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 

的UnfoldableDetailsFragment 89 =

public void openDetails(View coverView, Painting painting) { 
    ImageView image = (ImageView) coverView.findViewById(R.id.details_image); 
    TextView title = (TextView) coverView.findViewById(R.id.details_title); 
    TextView description = (TextView) coverView.findViewById(R.id.details_text); 

    Picasso.with(getActivity()).load(painting.getImageId()).into(image); ->89. LINE 
    Log.i("bilgi", "basildi"); 

    title.setText(painting.getTitle()); 

    SpannableBuilder builder = new SpannableBuilder(getActivity()); 
    builder 
      .createStyle().setFont(Typeface.DEFAULT_BOLD).apply() 
      .append(R.string.year).append(": ") 
      .clearStyle() 
      .append(painting.getYear()).append("\n") 
      .createStyle().setFont(Typeface.DEFAULT_BOLD).apply() 
      .append(R.string.location).append(": ") 
      .clearStyle() 
      .append(painting.getLocation()); 
    description.setText(builder.build()); 

    mUnfoldableView.unfold(coverView, mDetailsLayout); 
} 

PaintingAdapter:

public class PaintingsAdapter extends ItemsAdapter<Painting> implements View.OnClickListener { 

public PaintingsAdapter(Context context) { 
    super(context); 
    setItemsList(Arrays.asList(Painting.getAllPaintings(context.getResources()))); 
} 

@Override 
protected View createView(Painting item, int pos, ViewGroup parent, LayoutInflater inflater) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); 
    ViewHolder vh = new ViewHolder(); 
    vh.image = Views.find(view, R.id.list_item_image); 
    vh.image.setOnClickListener(this); 
    vh.title = Views.find(view, R.id.list_item_title); 
    view.setTag(vh); 

    return view; 
} 

@Override 
protected void bindView(Painting item, int pos, View convertView) { 
    ViewHolder vh = (ViewHolder) convertView.getTag(); 

    vh.image.setTag(item); 
    Picasso.with(convertView.getContext()).load(item.getImageId()).noFade().into(vh.image); 
    vh.title.setText(item.getTitle()); 
} 

@Override 
public void onClick(View view) { 
    //Log.i("bilgi", "basildi"); 
    UnfoldableDetailsFragment fm = new UnfoldableDetailsFragment(); 
    fm.openDetails(view, (Painting)view.getTag()); -->HERE IS 52. line 
    Log.i("bilgi", "basildi"); 

} 

private static class ViewHolder { 
    ImageView image; 
    TextView title; 
} 

}

回答

27

image是傳遞給into的目標。這是什麼null

確保您的佈局ID對於所有配置都是正確的,並指定@+id/details_image

+0

yeap image返回null,但所有配置正確 – 2014-11-07 07:54:46

+0

繼續做好工作。 – VVB 2016-06-21 04:00:14

0

也許painting.getImageId()是空的或無效?

或image或getActivity()? :)

+0

原來的實施是對活動我把它轉換到碎片,所以我改變一點點適配器的onClick方法從 @Override public void onClick(View view){if(view.getContext()instanceof UnfoldableDetailsActivity){ UnfoldableDetailsActivity activity =(UnfoldableDetailsActivity)view.getContext(); activity.openDetails(view,(Painting)view.getTag()); } } – 2014-11-06 22:19:29

0

我知道這有點舊,但是我只花了一大堆時間就忘了運行「setContentView」到圖像所在的佈局。

的ImageView的打算,直到它所處的主要佈局膨脹爲空;)

3

確保您充氣正確的XML 這是我的問題,因爲我複製一個已經存在的適配器和忘了改這

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(context) 
      .inflate(R.layout.list_item_subcategories, parent, false); 

我應該已經把佈局是list_item_categories

相關問題