java.lang.IllegalArgumentException:Target不能为null

时间:2015-09-15 05:22:46

标签: java android 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>

4 个答案:

答案 0 :(得分:0)

img2为空,您将其传递到into函数中。

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试将其更改为..

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布局,如果有img2,请检查ID。

答案 3 :(得分:0)

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