简体   繁体   中英

WebView - WebPage not available

I (like many others) followed the webview tutorial, but I can't get pages to load. Everything comes up as 'Webpage not Available'

I have ensured that the emulator does have inte.net access, and just to rule out a problem with the emulator I tried installing it on my phone, which resulted in the same behavior.

I have read that the biggest issue is people not putting the INTE.NET permission in my manifest file, which I have tried putting as a child of different elements in the manifest to no avail. Does anyone know why I can't get this to load?

Here is my code:

Manifest:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".AndroidTestActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <uses-permission android:name="android.permission.INTERNET" />
    </activity>     
</application>
</manifest>

AndroidTestActivity

public class AndroidTestActivity extends Activity {
    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.getSettings().setJavaScriptEnabled(true);

            webview.loadUrl("http://www.google.com/m");

            Intent intent = getIntent();
            // To get the action of the intent use
            System.out.println(intent.getAction());
            // We current open a hard-coded URL
            try {
                webview.setWebViewClient(new AndroidTestClient());

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
                webview.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

        private class AndroidTestClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
}

Thanks!

Your internet permission should be an immediate child of "manifest" - shouldn't be under "application".

eg

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage.name"
    android:installLocation="auto"
    android:versionCode="3210" android:versionName="1.1.0"> 

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="10"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

    <!-- activities go here -->

    </application>
</manifest>

Hope this helps -serkan

If you use VPN , it can lead to this error. I connected to VPN and started an emulator, then WebView showed an error:

在此处输入图像描述

I disconnected from the VPN, restarted the emulator, and web page loaded. Of course, I wrote <uses-permission android:name="android.permission.INTE.NET" /> in AndroidManifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM