Initiating an instance of PopupWindow as a class from a fragment in Android -


i've been getting null pointer exception when try , call showpopup2 firstfragment. showpopup class,

public class showpopup extends activity { public void showpopup2(view v) {     button btndismiss;     layoutinflater layoutinflater = (layoutinflater)showpopup.this.getsystemservice(context.layout_inflater_service);     view layout = layoutinflater.inflate(r.layout.popup_layout, null);     popupwindow popupwindow = new popupwindow(layout, 580, 500, true);      popupwindow.showatlocation(layout, gravity.center, 0, 40);     btndismiss=(button) layout.findviewbyid(r.id.btndismissxml);     btndismiss.setonclicklistener(new onclicklistener() {         @override     public void onclick(view arg0){         popupwindow.dismiss();         }     }); } }  

and fragment,

public class firstfragment extends fragment implements onclicklistener{ public static context context; button btnpopup; public showpopup showpopup;  @override public void oncreate(bundle saveddataentryinstancestate){     super.oncreate(saveddataentryinstancestate); }  //@override public view oncreateview(layoutinflater inflater{        view v = inflater.inflate(r.layout.first_fragment, container, false);        btnpopup = (button)v.findviewbyid(r.id.btnpopupxml);     btnpopup.setonclicklistener(this);     populatefirstfragment(v);     return v; }  //@override public void onviewcreated(view v) {     btnpopup = (button)v.findviewbyid(r.id.btnpopupxml);     btnpopup.setonclicklistener(this); }  //@override public void onclick(view v) {         showpopup.showpopup2(v);     }  } 

what causing null pointer exception? there better way structure this? goal able (1) access other methods in application as, (2) not have method cluttering fragments.
in advance....

you have declared instance of showpopup activity in firstfragment, have not initialized causing null pointer when call showpopup.showpopup2(v).
also, activity should not used object call functions on it. can put showpopup2() function inside firstfragment itself.

public class firstfragment extends fragment implements onclicklistener{ public static context context; button btnpopup;   @override public void oncreate(bundle saveddataentryinstancestate){ super.oncreate(saveddataentryinstancestate); }  //@override public view oncreateview(layoutinflater inflater{    view v = inflater.inflate(r.layout.first_fragment, container, false);    btnpopup = (button)v.findviewbyid(r.id.btnpopupxml); btnpopup.setonclicklistener(this); populatefirstfragment(v); return v; }  //@override public void onviewcreated(view v) { btnpopup = (button)v.findviewbyid(r.id.btnpopupxml); btnpopup.setonclicklistener(this); }  //@override public void onclick(view v) {     showpopup2(v); }   public void showpopup2(view v) { button btndismiss; layoutinflater layoutinflater = (layoutinflater)getactivity().getsystemservice(context.layout_inflater_service); view layout = layoutinflater.inflate(r.layout.popup_layout, null); popupwindow popupwindow = new popupwindow(layout, 580, 500, true);  popupwindow.showatlocation(layout, gravity.center, 0, 40); btndismiss=(button) layout.findviewbyid(r.id.btndismissxml); btndismiss.setonclicklistener(new onclicklistener() {     @override public void onclick(view arg0){     popupwindow.dismiss();     } }); }  } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -