WebView - 网页不可用
我(和其他许多人一样)遵循了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的互联网许可应该是“清单”的直接子级 - 不应该位于“应用程序”下。
例如
希望这有帮助
-塞尔坎
Your internet permission should be an immediate child of "manifest" - shouldn't be under "application".
e.g.
Hope this helps
-serkan
如果您使用
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: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.