使用 Picasso 从 URL 在自定义对话框中加载图像

Loading an iImage in a custom DialogBox from a URL with Picasso

我创建了一种方法,可以从 Drawable 文件夹加载图像,该方法工作正常,但是当我尝试从 URL 加载图像并将其设置为我的自定义布局的 Imageview,它 returns 只是:

java.lang.IllegalArgumentException: Target must not be null.

我的Java代码是:

public void CustomDialogLoadImage() {
    Dialog dialog = new Dialog(this);
    ImageView FF_ReceiptImage = (ImageView) dialog.findViewById(R.id.FF_ReceiptImage);
    Picasso.with(ConveyanceAmtUpdateApprove.this).load("http://i.imgur.com/DvpvklR.png").into(FF_ReceiptImage);
    dialog.setContentView(R.layout.custom_dialogbox_image);
    dialog.show(); 
}

我的自定义对话框是:

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

    <ImageView
        android:id="@+id/FF_ReceiptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:src="@drawable/mobile_sales_img"
        android:layout_gravity="center"
        android:background="@drawable/border"/>

</LinearLayout>

我已经尝试过可能的方法来解决这个问题,但它已解决请帮助我...

我再检查一遍后,我在第 4 行看到了奇怪的东西。

试试这个

public void CustomDialogLoadImage() {
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialogbox_image);
    ImageView FF_ReceiptImage = (ImageView) dialog.findViewById(R.id.FF_ReceiptImage);
    Picasso.with(ConveyanceAmtUpdateApprove.this).load("http://i.imgur.com/DvpvklR.png").into(FF_ReceiptImage);

    dialog.show(); 
}

或者您必须在已经先膨胀的 View 中执行此操作,这是示例代码

public class ImageDialog extends DialogFragment {

    public static final String ARG_IMAGE_URI = "arg_image_uri";
    public static final String ARG_IMAGE_NAME = "arg_image_name";
    public static final String ARG_IMAGE_ID_CHANNEL = "arg_image_id_channel";

    public ImageDialog(){ // if no argument you can use empty constructor }
    public static ImageDialog getInstance(String uriStr, String imageName, int idChannel){

        ImageDialog imageDialog = new ImageDialog();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_IMAGE_URI, uriStr);
        bundle.putString(ARG_IMAGE_NAME, imageName);
        bundle.putInt(ARG_IMAGE_ID_CHANNEL, idChannel);
        imageDialog.setArguments(bundle);
        return imageDialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       // inflate first with your own layout dialog 
       return inflater.inflate(R.layout.popup_image_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Look this part
        ImageView imageView = view.findViewById(R.id.popup_image);
        //-------------
        Bundle bundle = getArguments();
        if(bundle != null){
            String name = bundle.getString(ARG_IMAGE_NAME);
            getDialog().setTitle(name);
            String imageUri = bundle.getString(ARG_IMAGE_URI);
            int idChannel = bundle.getInt(ARG_IMAGE_ID_CHANNEL);
            if(idChannel == 0){
                Picasso.with(getContext())
                        .load(new File(imageUri))
                        .into(imageView);

            }else {
                Picasso.with(getContext())
                        .load(imageUri)
                        .into(imageView);
            }

        }

    }
}

并调用此对话框执行此操作

ImageDialog dialog = ImageDialog.getInstance(item.getImageUrl(), item.getName(), 1);
                dialog.show(fragmentManager, null);
public void CustomDialogLoadImage() {
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialogbox_image);
    ImageView FF_ReceiptImage = (ImageView) dialog.findViewById(R.id.FF_ReceiptImage); 
    dialog.show();
Picasso.with(ConveyanceAmtUpdateApprove.this).load("http://i.imgur.com/DvpvklR.png").into(FF_ReceiptImage);
}

会很好

问题是您正在尝试获取空视图

Dialog dialog = new Dialog(this);
View dialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialogbox_image,null,false);
ImageView dialogImage= dialogView.findViewById(R.id.FF_ReceiptImage);
Picasso.with(ConveyanceAmtUpdateApprove.this)
.load("http://i.imgur.com/DvpvklR.png")
.into(dialogViewImage);
dialog.setContentView(dialogView);
dialog.show();