ErlAl
ErlAl

Reputation: 439

Picasso android Target must not be null

I am pretty new to android studio and still learning from video tutorials. I found Picasso and want to use it for my project but when i tried to apply/practice in a listview i dont seem to get it, im having an error that say Target must not be null. Ive tried the solutions around but still nothing. Sorry for my bad english. here is my code please help me.

class MyAdapter extends ArrayAdapter<String> {

Context context;

public MyAdapter(Context context, int row_layout, String[] values) {
    super(context, R.layout.row_layout2, values);
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageHolder holder = null; 
    holder = new ImageHolder();

    LayoutInflater theInflater = LayoutInflater.from(getContext());
    View theView = theInflater.inflate(R.layout.row_layout2, parent, false);
    String tvShow = getItem(position);

    TextView theTextView = (TextView) theView.findViewById(R.id.textView1);

    theTextView.setText(tvShow);

    holder.imageIcon = (ImageView) convertView.findViewById(R.id.imageView1);

    convertView.setTag(holder);

    Picasso.with(this.context).load(getItem(R.drawable.sample4)).into(holder.imageIcon); //ERROR HERE

    return theView;
}

static class ImageHolder
{
    ImageView imageIcon;
}
}

MainActivity

public class MainActivity extends AppCompatActivity {

static class ImageHolder {
    ImageView imageIcon;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] favoriteShows = {
            "Hannibal", "Sherlock", "Supernatural"
    };

    //ImageHolder holder = new ImageHolder();

    ListAdapter theAdapter = new MyAdapter(this, R.layout.row_layout, favoriteShows);

    ListView theListView = (ListView) findViewById(R.id.theListView);

    theListView.setAdapter(theAdapter);

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            String tvShowPicked= "You selected " +
                    String.valueOf(adapterView.getItemAtPosition(position));

            Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
        }
    });

Ive alse tried to put the Picasso lines in MainActivity to see if anything changes.

 holder.imageIcon = (ImageView) findViewById(R.id.imageView1);

    ImageView imageView = (ImageView) findViewById(R.id.imageView1);

    Picasso.with(this)
            .load(R.drawable.sample4)
            .into(holder.imageIcon);

My row_layout2.xml

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="1dp"
    android:layout_marginTop="1dp"
    android:layout_marginRight="1dp"
    android:src="@drawable/sample3"
    android:id="@+id/imageView1"
    android:adjustViewBounds="true"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:textSize="10sp"
    android:textStyle="bold"
    android:textColor="#000000"
    android:gravity="left"
    android:paddingLeft="15dp">
</TextView>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:textSize="10sp"
    android:textStyle="bold"
    android:textColor="#000000"
    android:gravity="left|bottom"
    android:padding="15dp"
    android:text="ADDRESS HERE">
</TextView>

my activity_main.xml

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/theListView">

    </ListView>

Error Log:

06-29 21:06:13.990    1288-1288/com.erlawkward.albarol.listviewexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erlawkward.albarol.listviewexample/com.erlawkward.albarol.listviewexample.MainActivity}: java.lang.IllegalArgumentException: Target must not be null.

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

at android.app.ActivityThread.access$600(ActivityThread.java:141)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:137)

at android.app.ActivityThread.main(ActivityThread.java:5041)

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:793)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: Target must not be null.

at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)

at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)

at com.erlawkward.albarol.listviewexample.MainActivity.onCreate(MainActivity.java:59)

at android.app.Activity.performCreate(Activity.java:5104)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            
at android.app.ActivityThread.access$600(ActivityThread.java:141)
            
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            
at android.os.Handler.dispatchMessage(Handler.java:99)
            
at android.os.Looper.loop(Looper.java:137)
            
at android.app.ActivityThread.main(ActivityThread.java:5041)
            
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:793)
            
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            
at dalvik.system.NativeStart.main(Native 

Method)

Upvotes: 1

Views: 7058

Answers (3)

Omar Mainegra
Omar Mainegra

Reputation: 4184

In your adapter replace

Picasso.with(this.context).load(getItem(R.drawable.sample4)).into(holder.imageIcon);

with

Picasso.with(getContext()).load(R.drawable.sample4).into(holder.imageIcon); 

Edit

Also you are using the ViewHolder pattern in a wrong way. The whole point of using it is to avoid inflating layouts every time getView is called. I edited the method as follow

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageHolder holder;

    if (convertView == null){
        LayoutInflater theInflater = LayoutInflater.from(getContext());
        convertView = theInflater.inflate(R.layout.row_layout2, parent, false);

        holder = new ImageHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.textView1);
        holder.imageIcon = (ImageView) convertView.findViewById(R.id.imageView1);

        convertView.setTag(holder);
    }
    else{
        holder = (ImageHolder) convertView.getTag();
    }

    holder.textView.setText(getItem(position));

    //ERROR FIXED
    Picasso.with(getContext()).load(R.drawable.sample4).into(holder.imageIcon); 

    return convertView;
}

The Holder

static class ImageHolder
{
    ImageView imageIcon;
    TextView textView;
}

And onCreate in Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] favoriteShows = {
            "Hannibal", "Sherlock", "Supernatural"
    };

    ListAdapter theAdapter = new MyAdapter(this, R.layout.row_layout2, favoriteShows);

    ListView theListView = (ListView) findViewById(R.id.theListView);
    theListView.setAdapter(theAdapter);

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            String tvShowPicked = "You selected " +
                    String.valueOf(adapterView.getItemAtPosition(position));

            Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
        }
    });
}

Upvotes: 2

Seishin
Seishin

Reputation: 1477

Why do you use Picasso for loading a drawable into an ImageView? Just use the setImageResource() method of the ImageView.

Upvotes: 0

Soham
Soham

Reputation: 4417

The reason is your context is null.

Try to initialize your context.

public MyAdapter(Context context, int row_layout, String[] values) {
    super(context, R.layout.row_layout2, values);
    this.context=context;
}

Upvotes: 1

Related Questions