android java. lang.非法参数异常:目标不能为空

2sbarzqh  于 2023-01-19  发布在  Android
关注(0)|答案(4)|浏览(187)

当我传递图像的网址给毕加索然后它显示我错误"目标不能为空"。当我从网址使用毕加索然后它显示我的图像错误。甚至我的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>
db2dz4w8

db2dz4w81#

img2为空值,您要将其传递给into函数。
img2=(ImageView)findViewById(R.id.img2);之后添加一个null检查,以检查它何时停止为null,或者在测试时它是否仍为null,或者只是确认它为null。
检查您的布局ID的,并确保他们是正确的所有配置,并确保您有@+id/img2指定。

ehxuflar

ehxuflar2#

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

dxxyhpgq

dxxyhpgq3#

您不能使用毕加索库从https下载图像。您可以使用this库。

u1ehiz5o

u1ehiz5o4#

尝试将其更改为..

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。

相关问题