How to fix a webview error in android? - android

I'm trying to create a simple Android application that is just a webview. I've been following a tutorial (http://www.mkyong.com/android/android-webview-example/) but have adapted it to have just a webview instead of a button that opens a webview.
When I include the code, I get an error on the line saying "webview cannot be resolved or is not a field". Any ideas on how to troubleshoot? Full code is below:
Error where line occurs:
setContentView(R.layout.webview);
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
activity_main.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"
/>

Change setContentView(R.layout.webview) to setContentView(R.layout.actvity_main), or rename you xml file to webview.xml

You can do the same using xml or programaticaly as below
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //should be activity_main
webView = (WebView) findViewById(R.id.webView1);//find id of the view defined in activity_main
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
OR
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(MainActivity.this);// webview in mainactivity
setContentView(webView);// set the webview as the layout
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}

if you are following the example as you said then in the android manifest file remove android:theme="#android:style/Theme.NoTitleBar" /> and try to run it.

Related

How to use webView in read mode kotlin?

I have an app and i want to open the URL with Reading Mode
Without ads or something the reader don't need to see
How can i do that haw can i enable the reading mode on in kotlin ?
Try it with WebView,
In .xml file add this code:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
And inside activity(or fragment)
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}

Error:(15, 32) error: cannot find symbol variable webview

cannot find symbol webview
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview); //get the error here
WebView webview = (WebView) findViewById(R.id.miWebView);
webview.loadUrl("http://www.android.com");
}
In my activity_main.xml
<WebView
android:id="#+id/miWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</linearLayout>
I don´t know where is the error.
change
setContentView(R.layout.webview);
to
setContentView(R.layout.activity_main);
You must set activity_main.xml layout at setContentView(...)
use
setContentView(R.layout.activity_main);
to set layout of Activity because activity_main.xml is layout name instead of webview.xml.

WebView example android

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)
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)
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.
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
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");
}
}

"Unfortunately, has stopped" eclipse android avd

In the eclipse/android AVD, I get "Unfortunately, has stopped"
Help please.. here is the code
I just follow the other codes...
I tried everything I can I just don't know
what to do ...
help help help... not a total programmer ...
package com.somedomain.animatedinteractive;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled")
#SuppressWarnings("deprecation" )
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.action_settings);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("file:///android_asset/Parable_Book.swf");
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Your main layout does not have a WebView with id action_settings. Change
mWebView = (WebView) findViewById(R.id.action_settings);
to
mWebView = (WebView) findViewById(R.id.webview);
Also, when getting an "unfortunately app has stopped" message, it's a good idea to have a look at the exception stacktrace in logcat. Include it in your question, too.
package com.somedomain.animatedinteractive;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled")
#SuppressWarnings("deprecation" )
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("file:///android_asset/Parable_Book.swf");
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

Basic app with webview not working - FC on start

I am trying to create a simple webview and I am getting FC on each start.
Here is my MainActivity.java file:
package com.jerdog.apps;
import android.app.Activity;
import android.os.Bundle;
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 myWebView = (WebView) findViewById(R.layout.main);
myWebView.loadUrl("http://www.google.com");
}
}
and here is my res/layout/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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I am including my logcat from the moment I press Run in Eclipse at pastebin
http://pastebin.com/5trRw9Bd
This line:
WebView myWebView = (WebView) findViewById(R.layout.main);
is incorrect, and myWebView is null. Thus the following line throws the NullPointerException
Change the line to:
WebView myWebView = (WebView) findViewById(R.id.webview);
findViewById takes the id of the View you wish to find as an input, however you were passing in your layout. Changing this to the id of the WebView element from your main.xml file should fix the issue. There is a tutorial if you need a reference implementation.

Categories

Resources