【问题标题】:Picasso android Target must not be nullPicasso android 目标不能为空
【发布时间】:2017-02-15 18:38:52
【问题描述】:

我对 android studio 还是很陌生,还在从视频教程中学习。我找到了毕加索并想将它用于我的项目,但是当我尝试在列表视图中应用/练习时,我似乎没有得到它,我有一个错误说Target must not be null。我已经尝试了解决方案,但仍然没有。对不起,我的英语不好。这是我的代码,请帮助我。

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;
}
}

主活动

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();
        }
    });

我还尝试将毕加索的线条放入 MainActivity 以查看是否有任何变化。

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

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

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

我的 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>

我的activity_main.xml

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

    </ListView>

错误日志:

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 

方法)

【问题讨论】:

  • 发布布局文件代码

标签: android listview picasso


【解决方案1】:

在您的适配器中替换

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

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

编辑

您还以错误的方式使用了ViewHolder 模式。使用它的全部目的是避免每次调用 getView 时都膨胀布局。我将方法编辑如下

@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;
}

持有人

static class ImageHolder
{
    ImageView imageIcon;
    TextView textView;
}

以及在Activity中的onCreate

@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();
        }
    });
}

【讨论】:

  • 仍然有同样的错误。我的 holder.imageIcon 为空,我不知道为什么。
  • @ErlAl 你有没有把你的getView方法换成我的??,还要检查两种情况下的错误是一样的
  • 是的,先生,我已经更换了它,但仍然有同样的错误。
  • @ErlAl 我用你的布局测试过,效果很好,请发布日志和错误
  • 发布了日志.. 抱歉,我不知道如何在帖子中正确编辑。
【解决方案2】:

原因是你的context 为空。

尝试初始化你的上下文。

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

【讨论】:

  • 仍然有同样的错误。 Picasso.with(this.context).load(getItem(R.drawable.sample4)).into(holder.imageIcon);我的 .into() 中的值仍然为空
  • @omainegra's answer应该可以解决您的问题,如果没有,则可能存在与您的布局不匹配的 id。我的意思是说 imageView1 可能不存在于 row_layout2.xml 文件中。请仔细检查。
  • 这是我的图像视图
  • @ErlAl 请在你的问题中发布布局,全部
  • 如果这个答案没有解决您的问题,请参阅:stackoverflow.com/questions/26790301/…
【解决方案3】:

为什么要使用 Picasso 将可绘制对象加载到 ImageView 中? 只需使用 ImageView 的 setImageResource() 方法即可。

【讨论】:

  • 我尝试过使用它并使用不同的图像尺寸,但到目前为止我不喜欢输出。我希望我放置的所有图像都具有相同的尺寸并与设备成比例。如果您可以帮助我或给我小费,请
猜你喜欢
  • 1970-01-01
  • 2015-01-03
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 2018-01-29
相关资源
最近更新 更多