问题描述
我实现了一个的ListView
使用毕加索库2.4.0和我面临的一个问题。发生的事情:我推出使用Android Studio中的应用程序,然后我去上,我实现了的ListView
的特异性片段,一切看起来正常(所有图片加载中,同为在的TextView
),但如果我尝试向下滚动应用程序崩溃。这就是它告诉我:
I implemented a listView
using the Picasso Library 2.4.0 and I'm facing an issue. What happens: I launch the app using Android Studio, then I go to the specific fragment on which I implemented the listView
, everything looks ok ( all images are being loaded, same for the TextView
) BUT if I try to scroll down the app crashes. This is what it tells me:
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
这点我这一行:在com.zenyt.Volvo.Volvo1 $ SampleListViewAdapter.getView(Volvo1.java:106)
那就是: Picasso.with(上下文)
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;
}
}
该应用程序工作正常,直到我添加了第二个架
- > holder1
。如果我删除属于该所有行 holder1
应用程序的工作原理与没有问题。
The app was working fine until I added a second holder
-> holder1
. If I remove all lines that belong to this holder1
the app works with with no problem.
该sample_list_detail_item.xml
The 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>
对不起,我的英语水平。
Sorry for my english.
推荐答案
错误发生,因为 holder.image
为空 - 和成()
特别检查您传递的参数不为null。
The error occurs because holder.image
is null - and into()
specifically checks that the argument you pass in is not null.
当您使用 setTag()
/ getTag()
,只有一个对象可以被存储。因此,当你调用
When you use setTag()
/getTag()
, only a single object can be stored. Therefore when you call
view.setTag(holder);
view.setTag(holder1);
在架
中的数据没有被保存 - 它被替换 holder1
。相反,有多个 ViewHolder
的,你应该只添加一个字段到现有的 ViewHolder
类:
None of the data in holder
is saved - it is replaced with holder1
. Instead of having multiple ViewHolder
, you should just add another field to your existing 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);
这篇关于毕加索使用图书馆的目标不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!