1

so I have a working webview that is simple, but most of the work is done in the main activity. I am trying to break it out to its own little class so I can load other classes into the main activity on the fly. I am stuck on how to do this. I have tried this a few times and been debugging it but I just dont know if I am using the right findByValue(R.id.webView); thing here. it keeps giving me a

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference

issue. I have tried passing the reference in as a parameter and also hard coded it. I am missing something but dont know what. There is a WebView in my activity_main that is called wevView that is what I am trying to find out where i'm did mistake.

Thanks for any help with this.

So this is my webview class:

public class NewWebView {

//variables need for webview
private final String myPage = "http://www.google.com";
WebView myWebView = null;


//consturctor
public NewWebView(){}
//This method creates the WebView
public void createWebView(WebView webView)
{
    //WebView code
    //Find the Webview in the Activity.xml file
   //myWebView = webView;
    myWebView = (WebView)myWebView.findViewById(R.id.webview);
   // myWebView = (android.webkit.WebView)findViewById(R.id.webview);

    //Use the custom WebViewClient for internal app browsing
    myWebView.setWebViewClient(new MyWebViewClient());
    //now for enabling the settings
    WebSettings webSettings = myWebView.getSettings();
    //Enable JavaScript
    webSettings.setJavaScriptEnabled(true);
    //Enable andriod zoom features
    webSettings.setBuiltInZoomControls(true);
    //Loading the Url
    myWebView.loadUrl(myPage);

}


//Methods Used for WebViewer

//custom WebViewClent needed for 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 is Apivita remain in the APP
            return false;
        }
        /*
        //if it is not in my site redirect it to mobile browers
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
        */
        return true;
    }
}

//Enable the use of the system back button for navigation
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    //if the back button was pushed
    if((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
    {
        myWebView.goBack();
        return true;
    }
    //if not

    return  false;

}
}

and here is the 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 the webview
    myWebView = (android.webkit.WebView)findViewById(R.id.webview);
    NewWebView apWebView = new NewWebView();
    apWebView.createWebView(myWebView);

}
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
MNM
  • 2,673
  • 6
  • 38
  • 73
  • You should learn about inheritance... – OneCricketeer Feb 04 '16 at 11:19
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – OneCricketeer Feb 04 '16 at 11:21
  • 2
    But seriously, at `myWebView = (WebView)myWebView.findViewById(R.id.webview);` you never assign myWebView to anything, so it's null and that's your error – OneCricketeer Feb 04 '16 at 11:22

1 Answers1

1

It looks like you are finding your webview in the main activity and then passing it into your class. In your custom class, you're then trying to find another view, which you technically already have. So its trying to find a webview, inside of the webview. It obviously can't find this, so it's null.

In theory, you should be able to just use myWebView = webView;, however, you need to make sure that it is finding it in the first place.

SteveEdson
  • 2,485
  • 2
  • 28
  • 46