java - How to create a wrapper object for a webview -
so have working webview simple, of work done in main activity. trying break out own little class can load other classes main activity on fly. stuck on how this. have tried few times , been debugging dont know if using right findbyvalue(r.id.webview); thing here. keeps giving me
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.webkit.webview.findviewbyid(int)' on null object reference
issue. have tried passing reference in parameter , hard coded it. missing dont know what. there webview in activity_main called wevview trying find out i'm did mistake.
thanks this.
so webview class:
public class newwebview { //variables need webview private final string mypage = "http://www.google.com"; webview mywebview = null; //consturctor public newwebview(){} //this method creates webview public void createwebview(webview webview) { //webview code //find webview in activity.xml file //mywebview = webview; mywebview = (webview)mywebview.findviewbyid(r.id.webview); // mywebview = (android.webkit.webview)findviewbyid(r.id.webview); //use custom webviewclient internal app browsing mywebview.setwebviewclient(new mywebviewclient()); //now enabling settings websettings websettings = mywebview.getsettings(); //enable javascript websettings.setjavascriptenabled(true); //enable andriod zoom features websettings.setbuiltinzoomcontrols(true); //loading url mywebview.loadurl(mypage); } //methods used webviewer //custom webviewclent needed internal app navigatiion private class mywebviewclient extends webviewclient { public boolean shouldoverrideurlloading(android.webkit.webview view, string url) { if(uri.parse(url).gethost().equals(mypage)) { //this apivita remain in app return false; } /* //if not in site redirect mobile browers intent intent = new intent(intent.action_view, uri.parse(url)); startactivity(intent); return true; */ return true; } } //enable use of system button navigation public boolean onkeydown(int keycode, keyevent event) { //if button pushed if((keycode == keyevent.keycode_back) && mywebview.cangoback()) { mywebview.goback(); return true; } //if not return false; } } and here main activity
public class mainactivity extends appcompatactivity { webview mywebview = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //create webview mywebview = (android.webkit.webview)findviewbyid(r.id.webview); newwebview apwebview = new newwebview(); apwebview.createwebview(mywebview); } }
it looks finding webview in main activity , passing class. in custom class, you're trying find view, technically have. trying find webview, inside of webview. can't find this, it's null.
in theory, should able use mywebview = webview;, however, need make sure finding in first place.
Comments
Post a Comment