Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' null object reference -
i making app in feature user can upload images , video server working on activity want use in fragment. try code effectively, efficiently , not getting error during compiling , app runs when click on fragment shows "unfortunately app has stopped " , in log cat getting error:
java.lang.nullpointerexception: attempt invoke virtual method 'android.os.parcelable android.os.bundle.getparcelable(java.lang.string)' on null object reference
and points line:
public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); line ➦ fileuri = savedinstancestate.getparcelable("file_uri"); }
i don't know whats wrong
my code:
public class tabfragment4 extends fragment implements view.onclicklistener{ view parentholder; private static final string tag = mainactivity.class.getsimplename(); int req_code = 100; int video_code = 20; string path; uri selectedimageuri; // camera activity request codes private static final int camera_capture_image_request_code = 10; private static final int camera_capture_video_request_code = 200; public static final int media_type_image = 1; public static final int media_type_video = 2; context mcontext; private uri fileuri; // file url store image/video private imagebutton btncapturepicture, btnrecordvideo,gallerybtn , videobtn,b1,location; public tabfragment4() { // required empty public constructor } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { parentholder = inflater.inflate(r.layout.fragment_tab_fragment4, container, false); final locationmanager manager = (locationmanager) getactivity().getsystemservice(context.location_service); if ( !manager.isproviderenabled( locationmanager.gps_provider ) ) { alertdialog.builder malertdialog = new alertdialog.builder(getactivity()); // setting dialog title malertdialog.settitle("location not available, open gps?") .setmessage("activate gps use location service ?") .setpositivebutton("open settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent intent = new intent(settings.action_location_source_settings); startactivity(intent); } }) .setnegativebutton("cancel",new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }).show(); // call alert message } btncapturepicture = (imagebutton)parentholder.findviewbyid(r.id.btncapturepicture); btnrecordvideo = (imagebutton)parentholder. findviewbyid(r.id.btnrecordvideo); gallerybtn=(imagebutton)parentholder.findviewbyid(r.id.imagegallery); videobtn = (imagebutton)parentholder.findviewbyid(r.id.videogallery); location=(imagebutton)parentholder.findviewbyid(r.id.location); videobtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { video(); } }); gallerybtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // record video galleryimage(); } }); btncapturepicture.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // record video captureimage(); } }); /** * capture image button click event */ btnrecordvideo.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // record video recordvideo(); } }); /** * record video button click event */ btnrecordvideo.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // record video recordvideo(); } }); return parentholder; } public void video(){ intent intent = new intent(); intent.settype("video/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select file upload "), video_code); } public void galleryimage(){ intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select file upload "), req_code); } public string getpath(uri uri) { string[] projection = { mediastore.images.media.data }; cursor cursor = getactivity().managedquery(uri, projection, null, null, null); int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); } private void captureimage() { intent intent = new intent(mediastore.action_image_capture); fileuri = getoutputmediafileuri(media_type_image); intent.putextra(mediastore.extra_output, fileuri); // start image capture intent startactivityforresult(intent, camera_capture_image_request_code); } /** * launching camera app record video */ private void recordvideo() { intent intent = new intent(mediastore.action_video_capture); fileuri = getoutputmediafileuri(media_type_video); // set video quality intent.putextra(mediastore.extra_video_quality, 1); intent.putextra(mediastore.extra_output, fileuri); // set image file // name // start video capture intent startactivityforresult(intent, camera_capture_video_request_code); } public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); // save file url in bundle null on screen orientation // changes outstate.putparcelable("file_uri", fileuri); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); fileuri = savedinstancestate.getparcelable("file_uri"); } public void onactivityresult(int requestcode, int resultcode, intent data) { // if result capturing image if (requestcode == req_code) { selectedimageuri = data.getdata(); if (resultcode ==activity. result_ok) { path = getpath(selectedimageuri); launchupload(true); system.out.println("selectedpath1 : " + path); } } if (requestcode == video_code) { selectedimageuri = data.getdata(); if (resultcode == activity. result_ok) { path = getpath(selectedimageuri); launchupload(false); system.out.println("selectedpath1 : " + path); } } if (requestcode == camera_capture_image_request_code) { if (resultcode == activity.result_ok) { //successfully captured image // launching upload activity launchuploadactivity(true); //intent intent = new intent(this,uploadactivity.class); //startactivity(intent); } else if (resultcode ==activity. result_canceled) { // user cancelled image capture toast.maketext(getactivity(), "user cancelled image capture", toast.length_short) .show(); } else { // failed capture image toast.maketext(getactivity(), "sorry! failed capture image", toast.length_short) .show(); } } else if (requestcode == camera_capture_video_request_code) { if (resultcode == activity.result_ok) { // video recorded // launching upload activity launchuploadactivity(false); } else if (resultcode == activity.result_canceled) { // user cancelled recording toast.maketext(getactivity(), "user cancelled video recording", toast.length_short) .show(); } else { // failed record video toast.maketext(getactivity(), "sorry! failed record video", toast.length_short) .show(); } } } private void launchuploadactivity(boolean isimage) { intent = new intent(getactivity(), uploadactivity.class); i.putextra("filepath", fileuri.getpath()); i.putextra("isimage", isimage); startactivity(i); } private void launchupload(boolean isimage) { intent = new intent(getactivity(), uploadactivity.class); i.putextra("filepath", path); i.putextra("isimage", isimage); startactivity(i); } /** * ------------ helper methods ---------------------- * */ /** * creating file uri store image/video */ public uri getoutputmediafileuri(int type) { return uri.fromfile(getoutputmediafile(type)); } /** * returning image / video */ private static file getoutputmediafile(int type) { // external sdcard location file mediastoragedir = new file( environment .getexternalstoragepublicdirectory(environment.directory_pictures), config.image_directory_name); // create storage directory if not exist if (!mediastoragedir.exists()) { if (!mediastoragedir.mkdirs()) { log.d(tag, "oops! failed create " + config.image_directory_name + " directory"); return null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); file mediafile; if (type == media_type_image) { mediafile = new file(mediastoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg"); } else if (type == media_type_video) { mediafile = new file(mediastoragedir.getpath() + file.separator + "vid_" + timestamp + ".mp4"); } else { return null; } return mediafile; } @override public void onclick(view v) { } }
seems savedinstancestate == null
use this:
if (savedinstancestate != null) { fileuri = savedinstancestate.getparcelable("file_uri"); } else { fileuri = ??? - want in else case; }
Comments
Post a Comment