我想为所选图像设置自定义对话框的区域。如果我正在设置整个应用程序的背景图像,则下面的代码可以进行一些重新安排。出于某种原因,当我移动它来设置自定义对话框的区域时,我收到以下错误:
错误:
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
我在这里缺少什么?我已经读过这可以通过设置意图来解决 - 但我已经这样做了。任何帮助将不胜感激。
提前致谢
CODE:
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);
}
}
}
}
答案 0 :(得分:1)
执行该代码时:
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
并且您实际上有一个例外,代码会继续,您的imageStream
仍为空。
您应该更改代码以满足该条件并检查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的问题。
答案 1 :(得分:1)
11-03 18:19:05.216:E / AndroidRuntime(10428):at com.example.pictures.MainActivity.onActivityResult(MainActivity.java:133)
NullPointerExceptions非常简单,对细节的一点关注将解决它们。您不能在null上调用方法,也不能引用其字段。
例如,如果第133行是这一行:
Uri selectedImage = imageReturnedIntent.getData();
然后唯一可能的null实体将是imageReturnedIntent - 你需要处理它的null不同的情况,或者找出它为null的原因并防止它为null。无论哪种情况,最好理解为什么它是空的。
但要记住的是:使用堆栈跟踪;它告诉你究竟哪条线遇到了null。知道该行通常直接指向null实体;那么你有责任弄清楚如何处理null。