Nullpointerexception On Webview (webview.requestfocus())
Solution 1:
Ok guys, I solved this problem.
The clue to the solution was the screen orientation. Like you can see in the code of the first post, I called in the onCreate() method: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
The result of this call was, that the activity was called twice because of the orientation change. Thats the reason why I used:
if(Controller.unbuggedStart == true){
Controller.unbuggedStart = false;
webview.destroy();
} else{
Controller.unbuggedStart = true;
d = ProgressDialog.show(externalPlayer.this, "Loading...", "Wait a moment...");
webview.loadUrl(j.getCharSequenceExtra("link").toString());
}
This code prevents the WebView
for being started twice. You can see that I destroy one of the two created WebViews. Now, when I turned off the screen, the OS wants to call the destroyed WebView
which leads to the NullPointerException
.
I overthougt the mechanisms of my onCreate method and changed it. Now, the orientation is discribed in the manifest:
android:screenOrientation="landscape"
After this modification, the problem wasn't truley solved. When I turned off the screen, the OS launched the Activity with the WebView
in it again, because the orientation switched from Landscape to Portrait when I switched off the screen. I added in the Manifest the following:
android:configChanges="keyboardHidden|orientation"
and now everything works perfectly.
Solution 2:
Without knowing the version of Android that you're running, I took a look at the implementation of requestFocus() in the head revision of WebView and there don't appear to be that many opportunities for an NPE. One possibility is that when the screen is turned off, that the WebView.destroy() method is getting called (setting mWebViewCore to null), even though the Activity is still holding onto it and trying to give focus to it when the state is being restored.
You might try overriding onRestoreInstanceState in your Activity and seeing whether the WebView still has a consistent state at that point. Again, looking at the head revision, if you were to call "getSettings()", this will throw an NPE for the same reason and you could be relatively certain that this is the issue.
Post a Comment for "Nullpointerexception On Webview (webview.requestfocus())"