android - Null pointer on a imageview, don't know how to pass it to a view -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i have app takes picture , want see picture when taken. , works fine, picture , save storage. made view called "activity_image.xml. nullpointer:
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setimagebitmap(android.graphics.bitmap)' on null object reference
here try pass imageview main activity, , cant figure out how properly:
static final int request_take_photo = 1; static final int request_image_capture = 1; intent takepictureintent = new intent(mediastore.action_image_capture); private bitmap mimagebitmap; private string mcurrentphotopath; private imageview mimageview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fullscreen); mvisible = true; mcontrolsview = findviewbyid(r.id.fullscreen_content_controls); mcontentview = findviewbyid(r.id.fullscreen_content); sbutton = findviewbyid(r.id.sbutton); //final intent cam = new intent("android.media.action.image_capture"); // set user interaction manually show or hide system ui. mcontentview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { toggle(); } }); sbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view){ dispatchtakepictureintent(); } }); // upon interacting ui controls, delay scheduled hide() // operations prevent jarring behavior of controls going away // while interacting ui. findviewbyid(r.id.sbutton).setontouchlistener(mdelayhidetouchlistener); } private void dispatchtakepictureintent() { intent takepictureintent = new intent(mediastore.action_image_capture); // ensure there's camera activity handle intent if (takepictureintent.resolveactivity(getpackagemanager()) != null) { // create file photo should go file photofile = null; try { photofile = createimagefile(); } catch (ioexception ex) { // error occurred while creating file system.out.println("err cannot create file"); } // continue if file created if (photofile != null) { uri photouri = fileprovider.geturiforfile(this, "com.example.android.fileprovider", photofile); takepictureintent.putextra(mediastore.extra_output, photouri); startactivityforresult(takepictureintent, request_take_photo); } } } private file createimagefile() throws ioexception { // create image file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); string imagefilename = "jpeg_" + timestamp + "_"; file storagedir = getexternalfilesdir(environment.directory_pictures); file image = file.createtempfile( imagefilename, /* prefix */ ".jpg", /* suffix */ storagedir /* directory */ ); // save file: path use action_view intents mcurrentphotopath = "file:" + image.getabsolutepath(); return image; } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_image_capture && resultcode == result_ok) { try { mimagebitmap = mediastore.images.media.getbitmap(this.getcontentresolver(), uri.parse(mcurrentphotopath)); mimageview = (imageview)findviewbyid(r.id.imageview2); mimageview.setimagebitmap(mimagebitmap); } catch (ioexception e) { e.printstacktrace(); } } }
activity_image.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightsum="1"> <imageview android:layout_width="match_parent" android:layout_height="wrap_content" app:srccompat="@mipmap/ic_launcher" android:id="@+id/imageview2" android:layout_weight="0.95" /> </linearlayout>
mimageview = (imageview)findviewbyid(r.id.imageview2); mimagebitmap = mediastore.images.media.getbitmap(this.getcontentresolver(), uri.parse(mcurrentphotopath)); mimageview.setimagebitmap(mimagebitmap);
just reformate code this
change :
setcontentview(r.layout.activity_image);
Comments
Post a Comment