android - how to call a fragment method in an activity -
i have dialog method in fragment, want call main activity fab.
i tried call it, got error;
attempt invoke virtual method 'android.view.layoutinflater android.support.v4.app.fragmentactivity.getlayoutinflater()' on null object reference
editprofiledialog() dialog method in fragment tried call in main activity.
public class edit_fragment extends fragment { private editpresenter meditpresenter; public edit_fragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view = inflater.inflate(r.layout.edit_profile_fragment, container, false); butterknife.bind(this, view); meditpresenter = new editpresenter(this); meditpresenter.oncreate(); return view; } public void editprofiledialog(){ layoutinflater inflater = getactivity().getlayoutinflater(); view dialoglayout = inflater.inflate(r.layout.edit_profile_dialogue, null); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.setview(dialoglayout); builder.setcancelable(true); textview edit_profile = (textview)dialoglayout.findviewbyid(r.id.edit_me); typeface sms = typeface.createfromasset(getactivity().getassets(), "fonts/ftltlt.ttf"); edit_profile.settypeface(sms); textview new_profile_photo = (textview)dialoglayout.findviewbyid(r.id.addavatar); new_profile_photo.settypeface(sms); textview edit_username = (textview)dialoglayout.findviewbyid(r.id.editusernamebtn); edit_username.settypeface(sms); textview change_bio = (textview)dialoglayout.findviewbyid(r.id.statuslayout); change_bio.settypeface(sms); final alertdialog dialog = builder.create(); new_profile_photo.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view view) { dialog.dismiss(); editphotodialog(); } }); edit_username.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view view) { dialog.dismiss(); } }); change_bio.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view view) { dialog.dismiss(); } }); dialog.show(); } private void editphotodialog(){ layoutinflater inflater = getactivity().getlayoutinflater(); view dialoglayout = inflater.inflate(r.layout.edit_photo_dialogue, null); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.setview(dialoglayout); builder.setcancelable(true); framelayout camerabtn = (framelayout)dialoglayout.findviewbyid(r.id.camerabtn); framelayout gallerybtn = (framelayout)dialoglayout.findviewbyid(r.id.gallerybtn); appcompattextview camerabtntxt = (appcompattextview)dialoglayout.findviewbyid(r.id.camerabtntxt); typeface sms = typeface.createfromasset(getactivity().getassets(), "fonts/ftltlt.ttf"); camerabtntxt.settypeface(sms); appcompattextview gallerybtntext = (appcompattextview)dialoglayout.findviewbyid(r.id.gallerybtntext); gallerybtntext.settypeface(sms); final alertdialog dialog = builder.create(); camerabtn.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view view) { dialog.dismiss(); setcamerabtn(); } }); gallerybtn.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view view) { dialog.dismiss(); setgallerybtn(); } }); dialog.show(); } private void setgallerybtn() { if (permissionhandler.checkpermission(getactivity(), manifest.permission.read_external_storage)) { apphelper.logcat("read data permission granted."); new pickerbuilder(getactivity(), pickerbuilder.select_from_gallery) .setonimagereceivedlistener(imageuri -> { intent data = new intent(); data.setdata(imageuri); apphelper.logcat("new image select_from_gallery" + imageuri); meditpresenter.onactivityresult(this, appconst.select_profile_picture, result_ok, data); }) .setimagename(getactivity().getstring(r.string.app_name)) .setimagefoldername(getactivity().getstring(r.string.app_name)) .setcropscreencolor(r.color.colorprimary) .withtimestamp(false) .setonpermissionrefusedlistener(() -> { permissionhandler.requestpermission(getactivity(), manifest.permission.read_external_storage); }) .start(); } else { apphelper.logcat("please request read data permission."); permissionhandler.requestpermission(getactivity(), manifest.permission.read_external_storage); } } private void setcamerabtn() { if (permissionhandler.checkpermission(getactivity(), manifest.permission.camera)) { apphelper.logcat("camera permission granted."); new pickerbuilder(getactivity(), pickerbuilder.select_from_camera) .setonimagereceivedlistener(imageuri -> { apphelper.logcat("new image select_from_camera " + imageuri); intent data = new intent(); data.setdata(imageuri); meditpresenter.onactivityresult(this, appconst.select_profile_camera, result_ok, data); }) .setimagename(getactivity().getstring(r.string.app_name)) .setimagefoldername(getactivity().getstring(r.string.app_name)) .setcropscreencolor(r.color.colorprimary) .withtimestamp(false) .setonpermissionrefusedlistener(() -> { permissionhandler.requestpermission(getactivity(), manifest.permission.camera); }) .start(); } else { apphelper.logcat("please request camera permission."); permissionhandler.requestpermission(getactivity(), manifest.permission.camera); } } }
on fab.setonclicklistener() in main_activity. called editprofiledialog() this;
medit_profile_fragment.editprofiledialog();
and error showed in logcat.
change method this:
public void editprofiledialog(context context){ layoutinflater inflater=(layoutinflater)context.getsystemservice(context.layout_inflater_service); view dialoglayout = inflater.inflate(r.layout.edit_profile_dialogue, null); alertdialog.builder builder = new alertdialog.builder(context); builder.setview(dialoglayout); builder.setcancelable(true);
this because getactivity() not available in activity class.
Comments
Post a Comment