java - Preview captured Image NullpointerException -
public void opendialogtoaddreminder(final context context, final dbhelper dbhelper, final int rem_id, final int med_id) { layoutinflater layoutinflaterandroid = layoutinflater.from(context); final view mview = layoutinflaterandroid.inflate(r.layout.add_reminders_dialog, null); alertdialog.builder alertdialogbuilder = new alertdialog.builder(new contextthemewrapper(context, r.style.mydialog)); alertdialogbuilder.setview(mview); captureimage = (imagebutton) mview.findviewbyid(r.id.capture_image); captureimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { selectimage(context); } }); alertdialogbuilder .setcancelable(false) .setpositivebutton(dialog_title, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog alertdialog = alertdialogbuilder.create(); // show alertdialog.show(); } public void selectimage(final context context) { final charsequence[] items = { "take photo", "choose gallery", "cancel" }; android.app.alertdialog.builder builder = new android.app.alertdialog.builder(context); builder.settitle("add photo"); builder.setitems(items, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { boolean result= utility.checkpermission(context); if (items[item].equals("take photo")) { userchoosentask ="take photo"; if(result) cameraintent(context); } else if (items[item].equals("choose gallery")) { userchoosentask ="choose gallery"; if(result) galleryintent(context); } else if (items[item].equals("cancel")) { dialog.dismiss(); } } }); builder.show(); } public void galleryintent(context context) { log.i("context ",context.tostring()); intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content);// startactivityforresult(intent.createchooser(intent, "select file"),select_file); } public void cameraintent(context context) { intent takingpicturecameraintent = new intent(mediastore.action_image_capture); if (takingpicturecameraintent.resolveactivity(context.getpackagemanager())!=null) startactivityforresult(takingpicturecameraintent, request_camera); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == activity.result_ok) { if (requestcode == select_file) onselectfromgalleryresult(data); else if (requestcode == request_camera) oncaptureimageresult(data); } } public void oncaptureimageresult(intent data) { try{ bundle extras=data.getextras(); bitmap thumbnail = (bitmap) extras.get("data"); log.i("image camera bitmap ",thumbnail.tostring()); bytearrayoutputstream bytes=new bytearrayoutputstream(); thumbnail.compress(bitmap.compressformat.jpeg, 90,bytes); captureimage.setimagebitmap(thumbnail); savetogallery(thumbnail); } catch (exception e){e.printstacktrace();} }
add_reminders_dialog.xml
<?xml version = "1.0" encoding="utf-8"?> <relativelayout android:layout_marginleft="@dimen/activity_horizontal_margin" android:layout_marginright="@dimen/activity_vertical_margin" android:layout_margintop="@dimen/activity_horizontal_margin" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/r1" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.cardview android:id="@+id/cardview_medicine_image" android:layout_margintop="20dp" app:contentpadding="@dimen/activity_horizontal_margin" android:layout_below="@+id/cardview_medicine_time" android:layout_width="match_parent" android:layout_height="wrap_content"> <imagebutton android:layout_width="match_parent" android:layout_height="170dp" android:background="#424242" android:padding="10dp" android:clickable="true" android:src="@drawable/icon_camera" android:scaletype="fitcenter" android:id="@+id/capture_image"/> </android.support.v7.widget.cardview> </relativelayout>
getting nullpointerexception here:
captureimage.setimagebitmap(thumbnail);
i don't know why getting captureimage null. have define globally. why null have defined inside dialog box.
well captureimage
belongs view of first alert dialog (the 1 created in opendialogtoaddreminder
method.
i suspect upon clicking on captureimage
select image, dialog gets dismissed destroys captureimage
reference, hence when onactivityresult
called there no more captureimage
object.
you can check above if set dismiss listener on alertdialog.builder
e.g.
alertdialogbuilder.setondismisslistener(new ondismisslistener() { @override public void ondismiss(dialoginterface dialog) { log.d("ondismiss"); } });
place code right above:
alertdialog alertdialog = alertdialogbuilder.create();
edit:
if receive on logcat: "ondismiss" means assumption correct. try resolve by:
alertdialog.setcanceledontouchoutside(false);
Comments
Post a Comment