I'm a beginner in learning Android, please help me with this.
I keep getting this error:
02-19 16:55:00.330: E/AndroidRuntime(2335):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.mkyong.android/com.mkyong.android.WebViewActivity}:
java.lang.NullPointerException
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView mywebview = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mywebview.getSettings();
mywebview.loadUrl("http://www.google.com");
}
my xml file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I think it's this line
webView.getSettings().setJavaScriptEnabled(true);
You never say what webView is. You declare mywebview, but not webView
Related
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");
}
}
Newbie on mobile apps.
I'm trying to use webview on my android app. But there is a problem in my code. I used the example from https://developer.android.com/ but its now working somehow. Can you please check my code....
WebView myWebView = (WebView) findViewById(R.id.webview);
android says "cannot resolve symbol webview"...
whole code is here ->
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webview); // here is the problem
myWebView.loadUrl("http://www.example.com");
Check if you have a WebView in your activity_main.xml with an id attribute with value #+id/webview like in the example below:
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Make following changes to your webView in xml:
<WebView android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
android:id="#+id/webview"/>
I am trying to create an android app which will do nothing but would just redirect to the website . Now after opening the app if any button is clicked on it opens up in crome . How to stop that .
below is the onCreate() function in my Main Activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);`
below is the activity_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"
/>
It works with me.
Add this settings on your webView:
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new myWebViewClient());
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
And append this add this private class:
private class myWebViewClient extends WebViewClient {
}
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.
I want to open the following link in WebView
https://tickets.musiconelive.com/admin/SACValidateBarcode.asp
I am using following code to do that
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://tickets.musiconelive.com/admin/SACValidateBarcode.asp");
but it's not opening in WebView and instead is opening in the browser.
How can I fix this problem?
may this helps you
WebSettings mWebSettings;
WebView mWebView = (WebView)findViewById(R.id.services_detail_magnified_image);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
mWebView.setBackgroundColor(Color.TRANSPARENT);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
mWebView.loadUrl(StaticURL.uChangePassword);
mWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
//show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl (url);
return true;
}
}
i think this will help you.
package com.adySol;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class adySol extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="http://tickets.musiconelive.com/admin/SACValidateBarcode.asp";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(PluginState.ON);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(url);
}
}
Main.xml::
<?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"
android:background="#android:color/white"
>
<WebView android:id="#+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent"></WebView>
</LinearLayout>
Manifest permission :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
You need to set WebViewClient()
This is example in kotlin
webview.apply {
loadUrl("https://www.google.com/")
webViewClient = WebViewClient()
}
As per android documentation
public void setWebViewClient (WebViewClient client)
Sets the WebViewClient that will receive various notifications and requests. This will replace the current handler.
webView=findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR)
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(MyWebViewClient());
webView.loadUrl("https://google.com/");
class MyWebViewClient extends WebViewClient() {
#override
boolean shouldOverrideUrlLoading(WebView view , WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request)
}
}