我(像其他许多人一样)遵循了webview教程,但我无法加载页面。所有内容都显示为“网页不可用”
我已经确保模拟器确实可以访问互联网,并且为了排除模拟器的问题,我尝试将其安装在我的手机上,这导致了相同的行为。
我读到最大的问题是人们没有在我的清单文件中放置INTERNET权限,我曾尝试将其作为清单中不同元素的子元素,但无济于事。有人知道为什么我不能把这个装上车吗?
下面是我的代码:
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;
}
}
}谢谢!
发布于 2011-10-02 08:58:53
您的互联网权限应该是“清单”的直接子权限-不应该在“应用程序”下。
例如:
<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>希望这能对-serkan有所帮助
发布于 2022-02-09 20:07:12
如果您使用VPN,则可能会导致此错误。我连接到VPN并启动了一个模拟器,然后WebView显示错误:

我断开了与VPN的连接,重新启动了模拟器,并加载了网页。当然,我用AndroidManifest编写了<uses-permission android:name="android.permission.INTERNET" />。
https://stackoverflow.com/questions/7623802
复制相似问题