dev adding java android listview imageview picasso

java - adding - picasso android



El objetivo no debe ser nulo utilizando la Biblioteca Picasso (1)

Implementé listView usando la Biblioteca Picasso 2.4.0 y estoy enfrentando un problema. Qué sucede: inicio la aplicación con Android Studio, luego voy al fragmento específico en el que implementé listView , todo se ve bien (todas las imágenes se están cargando, lo mismo para TextView ) PERO si intento desplazarme hacia abajo, la aplicación falla . Esto es lo que me dice:

2-03 19:45:38.290 31501-31501/com.zenyt E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalArgumentException: Target must not be null. at com.squareup.picasso.RequestCreator.into(RequestCreator.java:553) at com.squareup.picasso.RequestCreator.into(RequestCreator.java:536) at com.zenyt.Volvo.Volvo1$SampleListViewAdapter.getView(Volvo1.java:106) at android.widget.AbsListView.obtainView(AbsListView.java:2445) at android.widget.ListView.makeAndAddView(ListView.java:1775) at android.widget.ListView.fillDown(ListView.java:678) at android.widget.ListView.fillGap(ListView.java:642) at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5525) at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3413) at android.widget.AbsListView.onTouchEvent(AbsListView.java:3901) at android.view.View.dispatchTouchEvent(View.java:7337) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2410) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2145) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468) at android.app.Activity.dispatchTouchEvent(Activity.java:2487) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063) at android.view.View.dispatchPointerEvent(View.java:7520) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4452) at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4503) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) at android.view.Choreographer.doCallbacks(Choreographer.java:555) at android.view.Choreographer.doFrame(Choreographer.java:523) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711

Me señala esta línea: at com.zenyt.Volvo.Volvo1$SampleListViewAdapter.getView(Volvo1.java:106) que es: Picasso.with(context)

public final class SampleListViewAdapter extends BaseAdapter { private final Context context; private final List<String> urls = new ArrayList<>(); private final List<String> models = new ArrayList<>(); private final List<String> price = new ArrayList<>(); public SampleListViewAdapter (Context context) { this.context = context; Collections.addAll(urls, Data.Volvo1URLS); Collections.addAll(models, Data.VolvoModels); Collections.addAll(price, Data.VolvoPrice); } @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; ViewHolder holder1; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.sample_list_detail_item, parent, false); holder = new ViewHolder(); holder1 = new ViewHolder(); holder.image = (ImageView) view.findViewById(R.id.photo); holder.text = (TextView) view.findViewById(R.id.url); holder1.text = (TextView) view.findViewById(R.id.price); view.setTag(holder); view.setTag(holder1); } else { holder = (ViewHolder) view.getTag(); holder1 = (ViewHolder) view.getTag(); } // Get the image URL for the current position. String url = getItem(position); String models = getItem1(position); String price = getItem2(position); holder.text.setText(models); holder.text.setTextSize(16); holder1.text.setText(price); holder1.text.setTextSize(16); // Trigger the download of the URL asynchronously into the image view. Picasso.with(context) .load(url) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) .centerInside() .tag(context) .into(holder.image); return view; } @Override public int getCount() { return urls.size(); } @Override public String getItem(int position) { return urls.get(position); } private String getItem1(int position) { return models.get(position); } private String getItem2(int position) { return price.get(position); } @Override public long getItemId(int position) { return position; } class ViewHolder { ImageView image; TextView text; } }

La aplicación funcionaba bien hasta que agregué un segundo holder -> holder1 . Si holder1 todas las líneas que pertenecen a este holder1 la aplicación funciona sin problemas.

El sample_list_detail_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="8dp" android:background="@android:color/black"> <ImageView android:id="@+id/photo" android:layout_width="@dimen/list_detail_image_size" android:layout_height="@dimen/list_detail_image_size" /> <TextView android:id="@+id/url" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:layout_gravity="start|center_vertical" /> <TextView android:id="@+id/price" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="start|center" /> </LinearLayout>

Lo siento por mi ingles.


El error ocurre porque holder.image es nulo, e into() comprueba específicamente que el argumento que pasa no es nulo.

Cuando use setTag() / getTag() , solo se puede almacenar un solo objeto. Por lo tanto, cuando llamas

view.setTag(holder); view.setTag(holder1);

Ninguno de los datos en el holder se guarda; se reemplaza por holder1 . En lugar de tener múltiples ViewHolder , solo debes agregar otro campo a tu clase ViewHolder existente:

class ViewHolder { ImageView image; TextView url; TextView price; }

Luego puede usar solo un único ViewHolder :

holder = new ViewHolder(); holder.image = (ImageView) view.findViewById(R.id.photo); holder.url = (TextView) view.findViewById(R.id.url); holder.price = (TextView) view.findViewById(R.id.price); view.setTag(holder);