Issue
I am trying to create a WebView dynamically using the following code:
mWebView = new WebView(this);
mWebView.setId(R.id.webview);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(mWebViewClient);
mWebView.setWebChromeClient(mWebChromeClient);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
But, when I run the program, my app force quits stating an error that there is no such method as 'setLayerType'. However, when I create the Webview via the xml, there seems to be no problem:
<WebView android:id="@+id/webview"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layerType="software" />
I use the 'layertype' attribute here and the app runs fine. Can somebody please explain the discrepancy? Is there no way to set the layer type of a WebView dynamically?
Solution
NOTE: The question "what API level do you build for?" is VERY different from the question "what is the minimum API level that you target?".
Given the behavior you have described, it suggests you are building with API level >= 11 and testing on a device that is API level < 11.
Because .setLayerType is only available from API level 11 onwards, building with API level >= 11 will build fine, but if you are not using compatibility tricks such as reflection or:
Compatibility.getCompatibility().setWebSettingsCache(webSettings);
...then when you test on a device that is API level <11 you will get a crash because that method is not supported. On the other hand, if you test on a device of API level >= 11 you should find it works.
Answered By - straya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.