在您将问题标记为重复之前,请阅读该问题...
所以我试图使用Intent从画廊中选择多个图像。当我选择多个图像时,它可以正常工作;而当我仅选择1个图像时,它会产生此错误。我寻找每个NullPointer异常问题,但都没有成功。
FATAL EXCEPTION: main
Process: com.blackhat.applocker, PID: 1892
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:10227 flg=0x1 }} to activity {com.blackhat.applocker/com.blackhat.applocker.ProtactGallery}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3839)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3882)
at android.app.ActivityThread.access$1300(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference
at com.blackhat.applocker.ProtactGallery.onActivityResult(ProtactGallery.java:63)
at android.app.Activity.dispatchActivityResult(Activity.java:6302)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3835)
这是我用于Intent的代码
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 2);
这是我的图像的ActivityResult方法内容
if(requestCode == 2){
if(data.getClipData().getItemCount() == 1){
Toast.makeText(this, "Only 1", Toast.LENGTH_SHORT).show();
}else{
for(int i=0;i<data.getClipData().getItemCount();i++){
path.add(data.getClipData().getItemAt(i).getUri());
Log.d("RjList",path.get(i).toString());
Random rn=new Random();
if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}
}
}
适用于多张图片。
当我仅选择1张图像时为什么不起作用? 任何建议都会被采纳。...
答案 0 :(得分:3)
我相信ClipData
仅在有多个要回送的Uris时返回。如果只有一个,您可以使用getData
来获得。您可以执行以下操作来解决该问题:
List<Uri> uris = new ArrayList<>();
ClipData cd = data.getClipData();
if ( cd == null ) {
Uri uri = data.getData();
uris.add(uri);
}
else {
for (int i = 0; i < cd.getItemCount(); i++) {
ClipData.Item item = cd.getItemAt(i);
Uri uri = item.getUri();
uris.add(uri);
}
}
答案 1 :(得分:1)
根据泰勒编码的建议,您可以对一张图像使用getData()方法,但仍然会产生错误。因此,您要做的就是在检查像这样的请求代码时检查data.getData()是否为空
if(requestCode == 2 && data.getData() !=null){
if(cd == null){
path.add(data.getData());
Random rn = new Random();
if(copyFileFromUri(this,data.getData(),String.valueOf(rn.nextInt(500)))){
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}else{
for(int i=0;i<data.getClipData().getItemCount();i++){
path.add(data.getClipData().getItemAt(i).getUri());
Log.d("RjList",path.get(i).toString());
Random rn=new Random();
if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}
}
}