webview browser not loading - android

I created a web view and im trying to just launch google.com for now.
Everything compiles right but when it opens it shows the default web page that says "Webpage not avaialable" The webpage at http://www.google.com might be temporarly down or it may have moved permantly to a new web address.
What am i missing to get this to open the web page? I tested it in the enumerator and on my phone. Both have web acess i can open google just fine from a browser on my phone.
Below is my webview.jave webview Manifest and Main.xml
package com.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState)
{
final Activity mActivity = this;
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById( R.id.webview );
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
mActivity .setTitle("Loading...");
mActivity .setProgress(progress * 100); //Make the bar disappear after URL is loaded
}
});
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webview"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".WebViewActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Main.xml
<?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/webview"
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"
/>
</LinearLayout>

You need an internet Permission in your manifest file.
<uses-permission android:name="android.permission.INTERNET"/>

Related

WebView error in android [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Resouce id is showing error and i dont know what wrong i have done
this is main java file
the line myWebView = (WebView) findViewById(R.id.webView); is showing error and if i changed the id to something else app is crashing in emulator and also in physical device
and its not about null pointer the user marked it as a duplicate i would request u to please improve your knowledge(no offence)
package com.piyush.gangatechnicalcampus;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.easyonlineconverter.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
here is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.piyush.gangatechnicalcampus.MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" >
<WebView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
tools:ignore="ObsoleteLayoutParam" />
</WebView>
</RelativeLayout>
and this is AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.piyush.gangatechnicalcampus">
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
</manifest>
try out this:
in xml file:
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
in java file:
private WebView webView;
webView = (WebView) findViewById(R.id.web_view);`
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://google.com");

WebView app not working on Motorola Defy (MB525) and/or Android 2.2.2

I have created and published a very simple Android app mainly consisting of a WebView. I haven't recieved any reports at all about crashes or bugs except from a friend of mine who can't even start the app. The phone in question is a Motorola Defy (MB525) running Android version 2.2.2. Every time my friend tries to open the app, an error message is displayed saying that the process has stopped unexpectedly, try again.
I have run the app on a virtual device, Motorola Droid running Android version 2.3.7, which was as close to my friend's phone as I could get but I didn't manage to reproduce the problem since the app ran smoothly. Are there any known issues with apps using WebView on Motorola Defy or Android version 2.2.2? Or can someone please give me any tips on how to get to the bottom of this problem?
MainActivity.java
package se.sebastianliljegren.inoff.info;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
//***Not used***
#SuppressWarnings("unused")
private boolean services() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else {
Toast.makeText(this, "Google Play Services ej tillgänglig", Toast.LENGTH_SHORT)
.show();
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//***AdMob Banner***
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
//***WebView start***
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
//Zoombuttons
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setUseWideViewPort(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.sebastianliljegren.se/inoff/android/lista.php");
//***Webview end***
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView myWebView = (WebView) findViewById(R.id.webView1);
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_above="#+id/adView"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</WebView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="*******************************" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.sebastianliljegren.inoff.info"
android:versionCode="3"
android:versionName="1.2" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />
<activity
android:name="se.sebastianliljegren.inoff.info.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>`
Didn't work due to the lack of support for Google Play Services on Android 2.2. Works now!

Android WebView shows black screen

I'm trying this example for the Android WebView:
http://www.linuxtopia.org/online_books/android/devguide/guide/tutorials/views/hello-webview.html
but I don't get what I want, here are my files:
MainActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webview;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
}
private class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sheya"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The fact is, when I launch my application, it shows a black screen, but if I remove the
webview.setWebViewClient(new WebClient());
line from application code, it opens the google page into the default browser...
-- EDIT --
Solved it, in the main.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" <!-- Instead of wrap_content -->
android:layout_height="fill_parent" <!-- Instead of wrap_content -->
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
How about if you try this
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}

Android WebView Screen Goes black during load the next or same page

We made an android application for samsung galaxy tab using webview but when we hit the same page or a different page in between we got a black screen for 4-5 seconds after that screen shows the page.
So please tell me what we do for this i don't want that black screen.
Kindly find the below code for the same.
CallKiosk.java
package one97.kiosk;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class CallKiosk extends Activity {
public final String url = "http://10.0.8.178:8088/Kiosk/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
setContentView(webview);
webview.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
}
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="wrap_content"
android:layout_height="wrap_content"
>
<WebView
android:id="#+id/webview"
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"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="one97.kiosk"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/logo" android:label="#string/app_name">
<activity android:name=".CallKiosk"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Try this if useful, webvieew.setBackgroundColor("#FFFFFF")

WebView is not opening webpages but browser is (ERROR: Web page not available)

I'm creating an Android application (Android 2.3.3) that has a header, a footer and a WebView between both. The problem is that the WebView is not opening any webpage. (NOTE: I'm running the app on an emulator).
I tried opening webpages using the Android browser and the webpages are opened correctly. I also tried:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
And the code works correctly (opens the page in the browser).
I've been working with www.google.com and with a domain of my own, I've also been working with the ip adress of both web pages (for google 72.14.204.147 and for my own, the ip of my own dev server).
Also, before the most popular answer is written, I already have <uses-permission android:name="android.permission.INTERNET" /> before the application tag.
I'm adding the code before anyone asks for it:
The activity java file:
public class MyActivity extends Activity {
//Global Variables
WebView mainWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadActivityViews();
mainWebView.getSettings().setJavaScriptEnabled(true);
mainWebView.loadUrl("www.google.com");
mainWebView.setWebViewClient(new MyWebViewClient());
}
/** Loads all global views of the Activity */
private void loadActivityViews(){
mainWebView = (WebView) findViewById(R.id.index_main_web_view);
}
//Internal Classes
/*
* MyWebView Class
*
* Forces links to open in the same webView
* Handles the back button.
* */
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
view.goBack();
return true;
}
return super.shouldOverrideKeyEvent(view, event);
}
}
}
The android Manifest:
(NOTE: it has "android.permission.INTERNET" before the application tag)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pixable.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MyActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
the main.xml (I don;t think its important, but I'm adding it just in case)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/index_main_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<RelativeLayout
android:id="#+id/index_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000" >
... My header butons ...
</RelativeLayout>
<WebView
android:id="#+id/index_main_web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/index_header"
android:layout_above="#+id/index_botom_layout" />
<LinearLayout
android:id="#+id/index_botom_layout"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_alignParentBottom="true"
android:background="#000000"
android:orientation="horizontal" >
... My Footer Butons ...
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I think your issue is you're not prepending the URL with http://. I bet you http://www.google.com works.

Categories

Resources