17

I'm trying to set image by using Picasso library on my project.

When I click image of the View,I'm getting an error on Picasso execution.

Logcat of the app

  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;
}

}

Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28

7 Answers7

46

image is the target passed to into. It is what's null.

Ensure that your layout IDs are correct for all configurations and specify @+id/details_image.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
6

make sure you inflating the correct xml That was my problem since I copied an already existing adapter and forgot to change that

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

Where the layout I should have put was list_item_categories

Jaad Chacra
  • 549
  • 7
  • 12
1

I know this is a little bit old, but one thing that I just spent a bunch of time on is forgetting to run "setContentView" to the layout that the image was on.

The ImageView is going to be null until the main layout that it lives on is inflated ;)

Dwebtron
  • 777
  • 13
  • 27
1

Check the ID of your imageview object is correct -

imageView=itemView.findViewById(R.id.imageview1);
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
Raj Godara
  • 21
  • 1
0

Maybe painting.getImageId() is empty or null?

Or image or getActivity()? :)

Serhii Nadolynskyi
  • 5,473
  • 3
  • 21
  • 20
  • The original implementation was on Activity I converted it to Fragment so I changed a little bit on Adapter onClick method from @Override public void onClick(View view) { if (view.getContext() instanceof UnfoldableDetailsActivity) { UnfoldableDetailsActivity activity = (UnfoldableDetailsActivity) view.getContext(); activity.openDetails(view, (Painting) view.getTag()); } } – Zafer Celaloglu Nov 06 '14 at 22:19
0

Check Id which you set in your layout file.

android:id="@+id/details_image"

Then,

 ImageView image = (ImageView) coverView.findViewById(R.id.details_image);
0
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.show_item, parent, false);
    return new ViewHolder(view);
}

Make sure that you select the right layout resource file, that's what caused the error for me.

Antoine
  • 1,393
  • 4
  • 20
  • 26