0

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"/>
0

3 Answers 3

2

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);    
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.