我已经创建了一个方法,我可以从Drawable文件夹加载图像,但是当我尝试从URL加载图像并将其设置在我的自定义布局的Imageview上时,它只返回:
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);
}
会很好的
问题是你试图获得一个null的视图
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();