我使用Picasso Library 2.4.0实现了listView
,我遇到了一个问题。会发生什么:我使用Android Studio启动应用程序,然后转到我实现listView
的特定片段,一切看起来都正常(所有图像都被加载,TextView
相同)但是如果我尝试向下滚动应用程序崩溃。这是它告诉我的:
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
它指出了这一行:at com.zenyt.Volvo.Volvo1$SampleListViewAdapter.getView(Volvo1.java:106)
这是: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;
}
}
应用程序运行正常,直到我添加了第二个holder
- &gt; holder1
。如果我删除属于此holder1
的所有行,那么该应用就可以正常使用。
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>
抱歉我的英文。
答案 0 :(得分:3)
发生错误是因为holder.image
为空 - 而into()
专门检查您传入的参数是否为空。
使用setTag()
/ getTag()
时,只能存储一个对象。因此,当你打电话
view.setTag(holder);
view.setTag(holder1);
holder
中的所有数据均未保存 - 它已替换为holder1
。您应该在现有的ViewHolder
类中添加另一个字段,而不是使用多个ViewHolder
:
class ViewHolder {
ImageView image;
TextView url;
TextView price;
}
然后您只能使用一个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);