Android : Fragments and Activities interconnections -


this question has answer here:

i facing problem in sending data fragment activity.....(always struggled fragments though)....i getting nullpointerexception here:

process: com.projects.techinfini.contricenter, pid: 3142 java.lang.nullpointerexception: attempt invoke virtual method 'void com.projects.techinfini.contricenter.mainactivity.onclick(java.lang.string)' on null object reference         @ com.projects.techinfini.contricenter.bottombar.onclick(bottombar.java:56)         @ android.view.view.performclick(view.java:4780)         @ android.view.view$performclick.run(view.java:19866)         @ android.os.handler.handlecallback(handler.java:739)         @ android.os.handler.dispatchmessage(handler.java:95)         @ android.os.looper.loop(looper.java:135)         @ android.app.activitythread.main(activitythread.java:5257)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 

here activity class , fragment class.....

mainactivity.java:

public class mainactivity extends appcompatactivity {       connectivitymanager connectivitymanager;     public webview webview;     private toolbar toolbar;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);           bottombar bottombar = (bottombar) getfragmentmanager().findfragmentbyid(r.id.fragment);           toolbar = (toolbar) findviewbyid(r.id.app_bar);         toolbar.setlogo(r.drawable.logo);         setsupportactionbar(toolbar);         toolbar.hideoverflowmenu();         getsupportactionbar().setdisplayshowtitleenabled(false);          connectivitymanager = (connectivitymanager)getsystemservice(getapplicationcontext().connectivity_service);           webview = (webview) findviewbyid(r.id.webview);         webview.setverticalscrollbarenabled(false);         webview.sethorizontalscrollbarenabled(false);             if(connectivitymanager.getnetworkinfo(0).isconnected()){               webview.loadurl("http://www.contricenter.com/index.php");          }          else {              alertdialog.builder builder = new alertdialog.builder(this);             builder.setcancelable(false);             builder.setmessage("no internet connection...please connect internet proceed.");             builder.setpositivebutton("ok", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int which) {                       webview.reload();                  }             });              alertdialog alertdialog = builder.create();              alertdialog.show();          }          webview.setwebviewclient(new webviewclient() {                                       @override                                      public boolean shouldoverrideurlloading(webview view, string url) {                                           return false;                                      }                                      }           );     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);          return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement           return super.onoptionsitemselected(item);     }      public void onclick(string url){          webview.loadurl(url);      } } 

bottombar.java(fragment):

    public class bottombar extends fragment implements view.onclicklistener {      imageview iv1,iv2,iv3,iv4,iv5;     webview wv;     mainactivity mainactivity;     public bottombar() {         // required empty public constructor     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view view = inflater.inflate(r.layout.fragment_bottom_bar, container, false);           return view;     }      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          iv1 = (imageview) getactivity().findviewbyid(r.id.imageview);         iv1.setonclicklistener(this);         iv2 = (imageview) getactivity().findviewbyid(r.id.imageview2);         iv3 = (imageview) getactivity().findviewbyid(r.id.imageview3);         iv4 = (imageview) getactivity().findviewbyid(r.id.imageview4);         iv5 = (imageview) getactivity().findviewbyid(r.id.imageview5);       }      @override     public void onclick(view v) {      string s = "any url";      mainactivity.onclick(s);         } } 

any appreciated , please tell me best way same....(doesn't matter if have write whole code again...)

what on 56th line of bottombar.java? seems provided not code. onclick(string url) called from? have mainactivity field not initialized. using somewhere.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -