gpt4 book ai didi

java - 选择图像时的 NPE

转载 作者:行者123 更新时间:2023-12-02 06:34:51 29 4
gpt4 key购买 nike

我想将自定义对话框的一个区域设置为所选图像。如果我设置整个应用程序的背景图像,下面的代码可以进行一些重新安排。由于某种原因,当我移动它来设置自定义对话框的区域时,我收到以下错误:

错误:

11-03 18:19:05.216: E/AndroidRuntime(10428): FATAL EXCEPTION: main
11-03 18:19:05.216: E/AndroidRuntime(10428): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/700 }} to activity {com.example.pictures/com.example.pictures.MainActivity}: java.lang.NullPointerException
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.os.Looper.loop(Looper.java:130)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.ActivityThread.main(ActivityThread.java:3691)
11-03 18:19:05.216: E/AndroidRuntime(10428): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:19:05.216: E/AndroidRuntime(10428): at java.lang.reflect.Method.invoke(Method.java:507)
11-03 18:19:05.216: E/AndroidRuntime(10428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-03 18:19:05.216: E/AndroidRuntime(10428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-03 18:19:05.216: E/AndroidRuntime(10428): at dalvik.system.NativeStart.main(Native Method)
11-03 18:19:05.216: E/AndroidRuntime(10428): Caused by: java.lang.NullPointerException
11-03 18:19:05.216: E/AndroidRuntime(10428): at com.example.pictures.MainActivity.onActivityResult(MainActivity.java:133)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.Activity.dispatchActivityResult(Activity.java:3950)
11-03 18:19:05.216: E/AndroidRuntime(10428): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-03 18:19:05.216: E/AndroidRuntime(10428): ... 11 more

我在这里缺少什么?我读到这可以通过设置 Intent 来解决 - 但我已经在这样做了。任何帮助将不胜感激。

提前致谢

代码:

public class MainActivity extends Activity {
private static final int SELECT_PHOTO = 100;
private Bitmap chosenBitmap;
final Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawingView drawing = new DrawingView(this);

Button button = (Button) findViewById(R.id.buttonShowCustomDialog);

// add button listener
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(context);
// custom dialog
//dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});


Button captureButton = (Button) dialog.findViewById(R.id.selectImage);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
dialog.show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
final Dialog dialog = new Dialog(context);

switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
ImageView imageview= (ImageView)dialog.findViewById(R.id.imageview);
chosenBitmap = yourSelectedImage;
Drawable res = new BitmapDrawable(yourSelectedImage);
imageview.setImageDrawable(res);
}
}
}

}

最佳答案

当您执行该代码时:

InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

实际上有一个异常,代码继续,并且您的 imageStream 仍然为 null。

您应该更改代码以满足此要求并检查 null:

if (imageStream != null) {
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
ImageView imageview= (ImageView)dialog.findViewById(R.id.imageview);
chosenBitmap = yourSelectedImage;
Drawable res = new BitmapDrawable(yourSelectedImage);
imageview.setImageDrawable(res);
}

或以其他适当的方式处理这种情况。

不过,在本例中,这可能不是导致 NPE 的问题。

关于java - 选择图像时的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759439/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com