在方法Picasso.with()中,Android应用中的目标不为空.load()

时间:2016-12-01 22:12:39

标签: java android photo picasso

我正在制作简单的 Android应用加载来自外部资源的照片,内部资源或openGL绘图。

我尝试用Picasso库加载照片 for Android,但遇到了:

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.gornik.choosephoto.MainActivity$1.onClick(MainActivity.java:62)
    at android.view.View.performClick(View.java:5697)
    at android.widget.TextView.performClick(TextView.java:10813)
    at android.view.View$PerformClick.run(View.java:22526)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

以下是我的MainActivity.java和activity_photo_from_external_resources.xml的代码:

    private RadioGroup radioGroup;
    private RadioButton radioButton;
    private Button submitBtn;
    private ImageView externalPhotoView;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

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

    public void onClickListener(){
        radioGroup = (RadioGroup)findViewById(R.id.allOptions);
        submitBtn = (Button)findViewById(R.id.button);

        submitBtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(radioGroup.getCheckedRadioButtonId() == -1){
                            Toast.makeText(MainActivity.this, "Select an option, please", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            int selectedOption = radioGroup.getCheckedRadioButtonId();
                            radioButton = (RadioButton) findViewById(selectedOption);
                            Toast.makeText(MainActivity.this,
                                    radioButton.getText().toString(),
                                    Toast.LENGTH_SHORT).show();
                            String verifyOption = radioButton.getText().toString();
                            switch (verifyOption) {
                                case "Photo from external resources":
                                    Intent intent = new Intent(MainActivity.this, PhotoFromExternalResourcesActivity.class);
                                    startActivity(intent);
                                    externalPhotoView = (ImageView)findViewById(R.id.externalImage);
                                    Picasso.with(getApplicationContext()).load(R.drawable.example_image).into(externalPhotoView);
                                    break;

                                case "Photo from Your resources":
                                    verifyStoragePermissions(MainActivity.this);
                                    Intent intent2 = new Intent(MainActivity.this, PhotoFromYourResourceActivity.class);
                                    startActivity(intent2);
                                    break;

                                case "OpenGL draw":
                                    Intent intent3 = new Intent(MainActivity.this, OpenGLDrawActivity.class);
                                    startActivity(intent3);
                                    break;
                            }
                        }
                    }
                }
        );
    }

    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }
}


    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gornik.choosephoto.PhotoFromExternalResourcesActivity">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/externalImage"
        android:src="@drawable/example_image"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

目标不为空的原因是什么?如何更正代码?

2 个答案:

答案 0 :(得分:0)

ImageView的引用必须为null:

 externalPhotoView = (ImageView)findViewById(R.id.externalImage);

这就是为什么你有例外:

  

IllegalArgumentException目标不能为空

尝试加载目标(.into(externalPhotoView))时:

Picasso.with(getApplicationContext()).load(R.drawable.example_image).into(externalPhotoView);

确保ImageView布局中确实存在标识为@+id/externalImage的{​​{1}}。

MainActivity正在膨胀activity_main.xml而不是activity_main.xml

答案 1 :(得分:0)

您只能访问当前活动中夸大的视图。

您正在尝试访问MainActivity中的externalImage,但externalImage中不存在activity_main.xml,因此externalPhotoView引用始终为空。

由于externalImage视图位于activity_photo_from_external_resources.xml,可能是在PhotoFromExternalResourcesActivity类中膨胀,因此您应该将Picasso相关代码移至PhotoFromExternalResourcesActivity类。