使用 Picasso Library 目标不能为空
Target must not be null using Picasso Library
我使用 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
-> 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>
对不起我的英语。
错误发生是因为 holder.image
为 null - 而 into()
专门检查您传入的参数是否为 null。
当您使用setTag()
/getTag()
时,只能存储一个对象。因此当你打电话给
view.setTag(holder);
view.setTag(holder1);
None holder
中的数据被保存 - 它被替换为 holder1
。与其拥有多个 ViewHolder
,不如将另一个字段添加到现有的 ViewHolder
class:
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);
我使用 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
-> 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>
对不起我的英语。
错误发生是因为 holder.image
为 null - 而 into()
专门检查您传入的参数是否为 null。
当您使用setTag()
/getTag()
时,只能存储一个对象。因此当你打电话给
view.setTag(holder);
view.setTag(holder1);
None holder
中的数据被保存 - 它被替换为 holder1
。与其拥有多个 ViewHolder
,不如将另一个字段添加到现有的 ViewHolder
class:
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);