Android webview未加载时出现问题
我对webview有一个不好的体验,它没有加载我请求的网页 我无法加载谷歌或任何其他网页的网络视图。我在xml中添加了:Android webview未加载时出现问题,android,webview,Android,Webview,我对webview有一个不好的体验,它没有加载我请求的网页 我无法加载谷歌或任何其他网页的网络视图。我在xml中添加了: <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_marginLeft="250px" android:layout_marginTop="80px" andr
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_marginLeft="250px"
android:layout_marginTop="80px"
android:layout_width="180px"
android:layout_height="160dip"
/>
这表明该网页不可用
我还将许可添加到清单中
我在此应用程序中有另一个活动,它使用以下方式加载youtube url:
startActivity(new Intent( Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=XS998HaGk9M")));// Starts an intent to watch the video
我不确定这可能是什么,我真的需要这方面的建议,因为我需要让它工作
谢谢
编辑:我也无法访问实际模拟器本身中的任何网页。通过在emulator内的搜索栏中进行搜索,连接到Google时也会显示相同的信息
我不知道为什么这会连接到youtube的意图,而不是一个网络视图
编辑:这甚至还没有连接到youtube,上面说的和上面一样。这是一团糟,因为我需要这个工作为我的项目明天。如果webview持续下降,这就不太可靠。我可能不得不更换我正在使用的设备,因为安卓系统总是出问题
编辑:几个小时后,我刚回来,没有接触代码或模拟器,当我运行应用程序时,youtube视频重新打开,我可以在模拟器中浏览。但我仍然无法通过webview连接。非常不可靠:(仿真器可以访问internet吗?我有时会注意到仿真器中出现类似的行为,这是因为仿真器没有正确启动。我唯一能想到的一个办法是重新启动仿真器,直到它可以访问internet为止(通常是一次或两次)
-Dan请确保您已包含访问Internet的权限
<uses-permission android:name="android.permission.INTERNET" />
我也遇到了同样的问题。将webview代码放在
MainActivity.onCreate()
方法之外的其他位置时,似乎可以解决这个问题。例如,将包含webview.loadUrl(…)的代码放在
进入按钮的onClick
方法。启动活动时,网络视图显示为空,单击按钮时,网络视图正确填充。package com.Example.Browser;
package com.Example.Browser;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.google.com/"); //Set Home page
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback on touch
//mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
//mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls
//mWebView.getSettings().setDisplayZoomControls(false);//Always Hide Zoom Controlls(Requires Api 11)
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)
mWebView.requestFocus(View.FOCUS_DOWN);//Enable WebView Interaction
//mWebView.setWebViewClient(new WebViewClient() {//Open URL on Error
//public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {//Open URL on Error
//mWebView.loadUrl("http://www.google.com");//Open URL on Error
//mWebView.loadUrl("file:///android_asset/error_404.jpg"); //Show Offline HTML file or Image on Error
// }
// });
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.KeyEvent;
导入android.view.view;
导入android.webkit.WebSettings;
导入android.webkit.WebSettings.PluginState;
导入android.webkit.CookieManager;
导入android.webkit.WebView;
导入android.webkit.WebSettings.RenderPriority;
导入android.webkit.WebViewClient;
公共类MainActivity扩展了活动{
私有网络视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieManager.getInstance().setAcceptCookie(true);//启用Cookie
mWebView=(WebView)findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//启用Java脚本
setWebViewClient(新的HelloWebViewClient());
mWebView.loadUrl(“http://www.google.com/“”;//设置主页
mWebView.setScrollBarStyle(View.SCROLLBARS_in_OVERLAY);//删除滚动条
mWebView.getSettings().setDefaultFontSize(12);//设置字体大小
mWebView.getSettings().setLoadsImagesAutomatically(true);//启用图像加载
mWebView.getSettings().setPluginState(pluginInstate.ON);//启用闪存
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);//改进触摸反馈
//mWebView.setBackgroundColor(0x00000000);//加载时显示透明屏幕
//mWebView.getSettings().SetBuilTinZoomControl(true);//设置缩放控件
//mWebView.getSettings().SetDisplayZoomControl(false);//始终隐藏缩放控件(需要Api 11)
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//设置缓存(8mb)
字符串appCachePath=getApplicationContext().getCacheDir().getAbsolutePath();//设置缓存(8mb)
mWebView.getSettings().setAppCachePath(appCachePath);//设置缓存(8mb)
mWebView.getSettings().setAllowFileAccess(true);//设置缓存(8mb)
mWebView.getSettings().setAppCacheEnabled(true);//设置缓存(8mb)
mWebView.getSettings().setCacheMode(WebSettings.LOAD_默认);//设置缓存(8mb)
mWebView.requestFocus(View.FOCUS_DOWN);//启用WebView交互
//mWebView.setWebViewClient(新WebViewClient(){//错误时打开URL
//public void onReceivedError(WebView视图,int-errorCode,字符串描述,字符串失败URL){//错误时打开URL
//mWebView.loadUrl(“http://www.google.com“”;//错误时打开URL
//mWebView.loadUrl(“file:///android_asset/error_404.jpg“”;//出错时显示脱机HTML文件或图像
// }
// });
}
私有类HelloWebViewClient扩展了WebViewClient{
@凌驾
公共布尔值shouldOverrideUrlLoading(WebView WebView,字符串url)
{
loadUrl(url);
返回true;
}
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件)
{
if((keyCode==KeyEvent.keyCode_BACK)&&mWebView.canGoBack())
{
mWebView.goBack();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
}
谢谢,不,emulator没有internet访问权限。但是我可以按照上述意图连接到youtube。我已多次重新启动emulator和我的计算机。谢谢。您在这方面有什么进展吗?
package com.Example.Browser;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.google.com/"); //Set Home page
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback on touch
//mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
//mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls
//mWebView.getSettings().setDisplayZoomControls(false);//Always Hide Zoom Controlls(Requires Api 11)
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)
mWebView.requestFocus(View.FOCUS_DOWN);//Enable WebView Interaction
//mWebView.setWebViewClient(new WebViewClient() {//Open URL on Error
//public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {//Open URL on Error
//mWebView.loadUrl("http://www.google.com");//Open URL on Error
//mWebView.loadUrl("file:///android_asset/error_404.jpg"); //Show Offline HTML file or Image on Error
// }
// });
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>