问题描述
- 不能够在webview中显示网页,出现Webpage not available
-
public class MainActivity extends Activity {
private String url = "http://www.hao123.com/";
private WebView webView;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
// Uri uri = Uri.parse(url);
// Intent intent = new Intent(Intent.ACTION_VIEW,uri);
// startActivity(intent);
init();
}
private void init() {
// TODO Auto-generated method stub
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
if(newProgress == 100){
closeDialog();
}
else{
openDialog(newProgress);
}
}private void closeDialog() { // TODO Auto-generated method stub if(dialog != null && dialog.isShowing()){ dialog.dismiss(); dialog = null; } } private void openDialog(int newProgress) { // TODO Auto-generated method stub if(dialog == null){ dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("正在加载"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(newProgress); dialog.show(); } else{ dialog.setProgress(newProgress); } } });
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
Toast.makeText(this,webView.getUrl(),Toast.LENGTH_SHORT).show();
if(webView.canGoBack()){
webView.goBack();
return true;
}
else{
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
}
注册文件已经加了权限。本来是可以加载网页的,制作了进度条判断页面的加载过程,就出错了。LOG显示08-25 03:01:29.029: E/cutils-trace(2289): Error opening trace file: No such file or directory (2)
解决方案
路径不正确,使用绝对路径试试
时间: 2024-11-17 03:11:44