Issue
I have tried to create a webview with JavaScript, but I get the error:
'Cannot cast View from WebView' on the line WebView myWebView = (WebView) findViewById(R.id.mainWebView);
This is the full code
package com.CalvaryChapelMelbourne.CCM;
import android.app.Activity;
import android.webkit.WebSettings;
public class WebView extends Activity {
WebView webview;
public WebView() {
WebView myWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
}
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainWebView"/>
Solution
I think your slightly confused. See below for correct Activity implementation:
package com.CalvaryChapelMelbourne.CCM;
import android.app.Activity;
import android.webkit.WebSettings;
public class WebViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView myWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
}
or if you wanted to construct a WebView object:
public WebView(Context context) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
WebView myWebView = (WebView) inflater.inflate(R.layout.mainWebView, null); // The xml file name not the id
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
Answered By - Blundell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.