I am trying to create a WebView dynamically using the following code:
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?
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:
…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.