【问题标题】:java.lang.IllegalArgumentException: Target must not be nulljava.lang.IllegalArgumentException:目标不能为空
【发布时间】:2022-05-19 20:08:40
【问题描述】:

当我将图像 url 传递给 Picasso 时,它会显示错误“目标不能为空”。当我使用 Picasso 从 url 获取图像时,它会显示错误。即使我分配给图像的 id 也是正确的。最后我用静态图片替换了图片网址,虽然我收到了同样的错误。

这是我的代码:

public class MainScreen extends AppCompatActivity {

private ImageView user_profile_pic,img2;
private ImageView like_button,dislike_button,location_button,refresh;

private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;

private SwipeFlingAdapterView flingContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);


    //Setting
    user_profile_pic=(ImageView)findViewById(R.id.profile_image);
    img2=(ImageView)findViewById(R.id.img2);

    //Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg").into(user_profile_pic);

    //Picasso.with(getApplicationContext()).load(android.R.drawable.btn_star).into(img);


    Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
            .placeholder(android.R.drawable.btn_star)
            .error(android.R.drawable.btn_star)
           .into(img2);


    flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);

    al = new ArrayList<>();
    al.add("php");
    al.add("c");
    al.add("python");
    al.add("java");
    al.add("html");
    al.add("c++");
    al.add("css");
    al.add("javascript");

    arrayAdapter = new ArrayAdapter<>(this, R.layout.custom_user_details, R.id.helloText, al );

    flingContainer.setAdapter(arrayAdapter);

    flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
        @Override
        public void removeFirstObjectInAdapter() {

            Log.d("LIST", "removed object!");
            al.remove(0);
            arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onLeftCardExit(Object dataObject) {

            Toast.makeText(getApplicationContext(),"Left !",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRightCardExit(Object dataObject) {

            Toast.makeText(getApplicationContext(),"Right !",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdapterAboutToEmpty(int itemsInAdapter) {

            al.add("XML ".concat(String.valueOf(i)));
            arrayAdapter.notifyDataSetChanged();
            Log.d("LIST", "notified");
            i++;
        }

        @Override
        public void onScroll(float scrollProgressPercent) {

            try
            {
                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
            }
            catch (NullPointerException e) {
                Log.e("tag", "NullPointerException" + e);
            }

        }
    });

    //flingContainer.getTopCardListener().selectRight(); To remove view from right side while pressing button

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这就是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="80">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/img2"
                android:src="@android:drawable/btn_star"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:padding="8dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Vimal, "
                    android:id="@+id/helloText"
                    android:textColor="@android:color/black"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="23"
                    android:textColor="@android:color/black"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:orientation="horizontal">


                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_weight="1">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:src="@android:drawable/btn_dialog"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:textSize="25sp"
                        android:layout_marginLeft="5dp"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                        android:gravity="center"/>

                </LinearLayout>


                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_weight="1">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:src="@android:drawable/btn_dialog"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:textSize="25sp"
                        android:layout_marginLeft="5dp"
                        android:layout_gravity="center"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>




    </LinearLayout>

    <View
        android:id="@+id/item_swipe_left_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:background="#A5F" />

    <View
        android:id="@+id/item_swipe_right_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:layout_gravity="right"
        android:background="#5AF" />

</FrameLayout>

【问题讨论】:

  • android.R.drawable.btn_star ?
  • 是的.. @Rustam 默认使用android的图像。
  • img2 是目标,所以你检查它是否为空?
  • 您使用的布局是否正确?如果是这样,请添加您的活动代码。
  • 只需清理并再次运行

标签: java android picasso


【解决方案1】:

img2 为 null,您将其传递给 into 函数。

img2=(ImageView)findViewById(R.id.img2); 之后添加一个空检查,以检查它何时停止为空,或者在您测试时它是否保持为空,或者只是为了确认它为空。

检查您的布局 ID 并确保它们适用于所有配置,并确保您指定了 @+id/img2

【讨论】:

  • 我尝试使用以下代码检查空值: if (img2==null) { img2.setImageResource(android.R.drawable.btn_star); } else { Picasso.with(getApplicationContext()).load("pbs.twimg.com/profile_images/596282530652753921/…) .placeholder(android.R.drawable.btn_star) .error(android.R.drawable.btn_star) .into(img2); }跨度>
  • 然后我在 logcat 中收到此错误:尝试在空对象引用上调用虚拟方法 'void android.widget.ImageView.setImageResource(int)'
  • 你上面的评论清楚地表明你的img2null
  • @VimalDiwani 在确保 img2 为空后,您正尝试调用函数 setImageResource()...当然您会遇到该错误!关键是只有在它不为空时才这样做。如果它为空,那么你需要检查你的 ID 和东西。
【解决方案2】:

您确定加载正确的布局吗?因为您在 Java 文件中有 R.id.profile_image,但我没有在您的 xml 文件中找到该 id。

【讨论】:

  • 他在 picasso.load() 中使用的是R.id.img2 而不是R.id.profile_image
  • 是的,我知道,但我问他确定他加载了正确的布局?因为在他的java代码中有: user_profile_pic=(ImageView)findViewById(R.id.profile_image);并且在他的 xml 中它不包含该 id,即使有 id img2 。
【解决方案3】:

您无法使用 Picasso 库从 https 下载图片。 您可以使用this 库。

【讨论】:

  • 我发现即使您使用https 协议也能正常工作,但是当任何 URL 重定向到另一个 https URL 时它不起作用。
【解决方案4】:

尝试将其更改为..

Picasso.with(MainScreen.this).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
            .placeholder(android.R.drawable.btn_star)
            .error(android.R.drawable.btn_star)
           .into(img2);

还要确保您使用的是 activity_main_screen.xml 布局并检查 id 是否存在 img2。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2019-07-28
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多