java - Camera not working -


i have mainactivity in have function related camera. when calling camera open function mainactivity working fine. when calling main activity camera function through adaptor giving below error:-

1-10 15:53:48.494 5741-5741/user.com.test2 e/inputeventreceiver: exception dispatching input event. 11-10 15:53:48.494 5741-5741/user.com.test2 e/messagequeue-jni: exception in messagequeue callback: handlereceivecallback 11-10 15:53:48.494 5741-5741/user.com.test2 e/messagequeue-jni: java.lang.nullpointerexception: attempt invoke virtual method 'android.app.activitythread$applicationthread android.app.activitythread.getapplicationthread()' on null object reference @ android.app.activity.startactivityforresult(activity.java:3794) @ android.support.v4.app.basefragmentactivityjb.startactivityforresult(basefragmentactivityjb.java:48) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:75) @ android.app.activity.startactivityforresult(activity.java:3755) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:871) @ user.com.hlthee.reminders.galleryintent(reminders.java:1049) @ user.com.hlthee.reminders$24.onclick(reminders.java:1033) @ com.android.internal.app.alertcontroller$alertparams$3.onitemclick(alertcontroller.java:1082) @ android.widget.adapterview.performitemclick(adapterview.java:305) @ android.widget.abslistview.performitemclick(abslistview.java:1146) @ android.widget.abslistview$performclick.run(abslistview.java:3072) @ android.widget.abslistview.ontouchup(abslistview.java:3920) @ android.widget.abslistview.ontouchevent(abslistview.java:3657) @ android.view.view.dispatchtouchevent(view.java:8476) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2400) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2093) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2406) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2107) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2406) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2107) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2406) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2107) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2406) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2107) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2406) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2107) @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:2372) @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1719) @ android.app.dialog.dispatchtouchevent(dialog.java:765) @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:2333) @ android.view.view.dispatchpointerevent(view.java:8671) @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:4171) @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:4037) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3587) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3640) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3606) @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:3723) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3614) @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:3780) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3587) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3640) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3606) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3614) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3587) @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:5855) @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:5829) @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:5800) @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:5945) @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:185) @ android.os.messagequeue.nativepollonce(native method) @ android.os.messagequeue.next(messagequeue.java:143) @ androi 11-10 15:53:48.494 5741-5741/user.com.test2 d/androidruntime: shutting down vm

calling adaptor:-

 @override     public void onbindviewholder(final medicineadaptor.viewholder holder, final int position) {           holder.remindercardview.setonlongclicklistener(new view.onlongclicklistener() {             @override             public boolean onlongclick(view v) {                  log.i("on long pressed","yes");                 //toast.maketext(context,integer.tostring(listmedicine.getid()),toast.length_long).show();                  //creating instance of popupmenu                  final popupmenu popupmenu=new                         popupmenu(context,holder.remindercardview);                  //inflating popup using xml file                 popupmenu.getmenuinflater().inflate(r.menu.reminder_options,popupmenu.getmenu());                  //registering popup onmenuitemclicklistener                  popupmenu.setonmenuitemclicklistener(new popupmenu.onmenuitemclicklistener(){                       @override                     public boolean onmenuitemclick(menuitem item) {                          if (item.gettitle().equals("edit details"))                         {                               //edit details                             //((addreminder)mactivity).opendialogtoaddreminder();                             reminders reminders=new reminders();                             reminders.opendialogtoaddreminder(context, dbhelper, listmedicine.getremender_id(),listmedicine.getid());                              update(listmedicine);                          }                                                return false;                     }                 });                  popupmenu.show(); //showing popup menu                   return false;             }         });      } 

mainactivity.java

public void cameraintent(context context)     {         intent takingpicturecameraintent = new intent(mediastore.action_image_capture);         **if (takingpicturecameraintent.resolveactivity(getpackagemanager())!=null)**             startactivityforresult(takingpicturecameraintent, request_camera);     } 

logcat showing error @ highlighted line above:-

research done:- able figure out using google , stackoverfollow. because of context.

so tried context.getpackagemanager()

but doing camera not working in mainactivity also.

edit:--

mainactivity.java

  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();}      } 

updated:-

public void cameraintent(context context)     {         intent takingpicturecameraintent = new intent(mediastore.action_image_capture);         if (context.getclass().getsimplename().equals("listreminders"))         {             context.**startactivityforresult()** // bolded region red color. means no fuction found.          }         else         {             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);** // getting nullpointerexception             savetogallery(thumbnail);         }         catch (exception e){e.printstacktrace();}      } 

give try this.
since calling cameraintent adapter class. , in method startactivtyforresult() method of activity class. need call context.startactivtyforresult() instead of calling startactivtyforresult() when calling adapter.

you can achieve adding calltype parameter in opendialogtoaddreminder

if(calltype.equals("fromactivity")) { startactivtyforresult(....); } else {   ((activity)context).startactivityforresult();     } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -