Question

There is a error in mView, I need solution

package com.example.account;

import android.app.Activity;
import android.os.Bundle;

public class WebView extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

}

}

My xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Cannot cast from view to webview

WebView mView = (WebView) findViewById(R.id.webView1)
Was it helpful?

Solution 2

You are using your activity name as WebView. This name is already used by SDK API. This is the reason it is giving you error.

To solve it, just rename your WebView.java file to some another name like MyWebView.java then your problem will surely solve.

To safely rename your .java file, Just go to packageexplorer , select WebView.java and press F2, and give new name.

OTHER TIPS

Try this layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

give me feedback on this.

Update: You should change your Activity name with Different one. or defined your WebView with different name like:

WebView mView2 = (WebView) findViewById(R.id.webView1)

Try this, it may help

1 . Using Eclipse, create a new Android project and name it as WebView.

2 . Add the following statements to the main.xml file:

<?xml​ version=”1.0” ​encoding=”utf-8”?>
<LinearLayout ​xmlns:android=”http://schemas.android.com/apk/res/android” ​​​​android:orientation=”vertical” ​​​​
android:layout_width=”fill_parent” ​
​​​android:layout_height=”fill_parent” ​​​​>

<WebView
android:id=”@+id/webview1” ​​
​​android:layout_width=”wrap_content” ​
​​​android:layout_height=”wrap_content” />

</LinearLayout> 

3 . In the MainActivity.java file, add the following statements in bold:

package​ com.emergingandroidtech.WebView;
import​ android.app.Activity;
import ​android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public ​class ​MainActivity​ extends​ Activity
​{
​​​​
/**​Called​ when​ the ​activity ​is ​first ​created.​*/

​@Override ​​
​​public ​void ​onCreate(Bundle​savedInstanceState)​
{
​​​​​​​​super.onCreate(savedInstanceState);
​setContentView(R.layout.main);
​​​​​​​​
WebView wv = (WebView) findViewById(R.id.webview1);
​​​​​​​​WebSettings webSettings = wv.getSettings(); ​​
​​​​​​webSettings.setBuiltInZoomControls(true);
​​​​​​​​wv.loadUrl( ​​​​​​​​“www.google.com”);
​​​​}
}

4. Don't forget to give the internet permission in manifest file

Thank you

You should keep webview inside any parent view

Android WebView Example

  • Do not use reserved words as your variable, class and method name.
  • Here, WebView is reserved or already used by SDK. If you use that word again, Compiler will get confused.

activity_main.xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity
{
private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webview=(WebView)findViewById(R.id.webview);
//loads androidride homepage to webview
webview.loadUrl("https://www.androidride.com");
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top