WebView - 网页不可用

发布于 2024-12-08 01:51:28 字数 2326 浏览 0 评论 0原文

我(和其他许多人一样)遵循了 webview 教程,但我无法加载页面。一切都显示为“网页不可用”

我已确保模拟器确实可以访问互联网,并且为了排除模拟器的问题,我尝试将其安装在手机上,这导致了相同的行为。

我读到最大的问题是人们没有将 INTERNET 权限放入我的清单文件中,我尝试将其作为清单中不同元素的子元素放入,但没有成功。有谁知道为什么我无法加载此内容?

这是我的代码:

清单:

<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;
            }
        }
}

谢谢!

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 internet 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 INTERNET 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-12-15 01:51:28

您的互联网许可应该是“清单”的直接子级 - 不应该位于“应用程序”下。

例如

<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>

希望这有帮助
-塞尔坎

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

e.g.

<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

半衬遮猫 2024-12-15 01:51:28

如果您使用 VPN,可能会导致此错误。我连接到 VPN 并启动模拟器,然后 WebView 显示错误:

在此处输入图像描述

我断开与 VPN 的连接,重新启动模拟器并加载网页。当然,我在AndroidManifest中写了

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

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文