Reputation: 940
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"/>
Upvotes: 0
Views: 3941
Reputation: 24181
I think you are new in Android development, We DONT DEFINE A CONSTRUCTOR OF ACTIVITY , you should override the method onCreate(Bundle icircle);
like this :
public class MyActivity extends Activity {
WebView webview;
@Override
public void onCreate(Bundle icircle) {
super(icircle);
this.setContentView(R.layout.main);
WebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = WebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
}
NOTE : Dont rename your activities with names of UIElements or Classes like WebView , TextView ...etc
Upvotes: 1
Reputation: 156
You are trying to define a class WebView as a subclass of Activity but WebView is already a subclass of View ( http://developer.android.com/reference/android/webkit/WebView.html )
The method findViewById(..) of Activity will return a View which you cannot cast to an Activity.
You have to rename your class (is that class the class you launch at the beginning?)
Try like this:
public class MyWebViewActivity extends Activity {
/** Called when the activity is first created. */
@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);
}
}
You also have to adjust your AndroidManifest.xml.
Upvotes: 1
Reputation: 76544
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);
}
Upvotes: 2