|
查看: 2498|回复: 17
|
android webviewclient
[复制链接]
|
|
|
各位好,
我是android apps 的新手。。
我现在有个webview,当网页出现error的时候,例如error 404, 我想出现一些信息或转去其他的link.
但是它始终没有去到public void onReceivedError 这里。。。
不知各位有没有任何建议呢?
谢谢
- package com.yes;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class LoadHome extends Activity {
- /** Called when the activity is first created. */
- private NetworkInfo networkInfo;
- private ConnectivityManager connectManager;
- private AlertDialog alertdialog;
- private WebView webView;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if(savedInstanceState != null){
- ((WebView)this.findViewById(R.id.webview2)).restoreState(savedInstanceState);
- }
-
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
- this.getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
-
- setContentView(R.layout.main);
-
- /*check for active network connection*/
- boolean connected;
- connectManager = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
- networkInfo = connectManager.getActiveNetworkInfo();
- if(networkInfo == null){
- connected = false;
- }else{
- if(networkInfo.getState() == NetworkInfo.State.CONNECTED){
- connected = true;
- }else{
- connected = false;
- }
- }
- /*end*/
- if(connected){
-
- final Activity activity = this;
- final ProgressDialog progressDialog = new ProgressDialog(activity);
- progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- progressDialog.setMessage("Loading...");
- progressDialog.setCancelable(false);
-
- webView = (WebView)this.findViewById(R.id.webView1);
- webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
-
- WebSettings websettings = webView.getSettings();
- websettings.setJavaScriptEnabled(true);
- websettings.setBuiltInZoomControls(true);
- websettings.setUseWideViewPort(true);
- //webView.requestFocus(View.FOCUS_DOWN);
-
- webView.setInitialScale(1);
-
- webView.setWebChromeClient(new WebChromeClient() {
- public void onProgressChanged(WebView view, int progress) {
- progressDialog.show();
- progressDialog.setProgress(0);
- progressDialog.incrementProgressBy(progress);
- if(progress == 100 && progressDialog.isShowing())
- progressDialog.dismiss();
- }
- });
-
- webView.setWebViewClient(new WebViewClient() {
-
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url)
- {
- if(!url.equals(Configuration.home)){
- Intent i = new Intent();
- Bundle bun = new Bundle();
- bun.putString("url", url);
- i.setClassName("com.yes", "com.yes.LoadPage");
- i.putExtras(bun);
- startActivity(i);
- finish();
- }else{
- view.loadUrl(url);
- }
- return true;
- }
-
- public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
- // TODO Auto-generated method stub
- Log.i("page error", String.valueOf(errorCode));
- }
- });
-
- webView.loadUrl(Configuration.home);
- }else{
- alertdialog = new AlertDialog.Builder(this).create();
- alertdialog.setMessage(Configuration.nointernetmsg);
- alertdialog.setButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- System.runFinalizersOnExit(true);
- System.exit(0);
- }
- });
- alertdialog.show();
-
- }
-
- }
-
- public void onSaveInstanceState(Bundle outState){
- webView.saveState(outState);
- }
-
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 23-8-2011 04:51 PM
|
显示全部楼层
回复 1# jay_goh
try catch 试过吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 23-8-2011 05:12 PM
|
显示全部楼层
回复 2# win7qi
谢谢你,我完成了那个project了。。  |
|
|
|
|
|
|
|
|
|
|
发表于 23-8-2011 05:13 PM
|
显示全部楼层
setWebViewClient 和 setWebViewClient有一样吗?
我看SDK都是 setWebViewClient 的
那 shouldOverrideUrlLoading 有正常工作吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 23-8-2011 05:28 PM
|
显示全部楼层
setWebViewClient 和 setWebViewClient有一样吗?
我看SDK都是 setWebViewClient 的
那 shouldOverride ...
jackhui 发表于 23-8-2011 05:13 PM 
setWebViewClient 和 setWebViewClient有什么不一样吗? shouldOverrideUrlLoading 有正常操作。 |
|
|
|
|
|
|
|
|
|
|
发表于 23-8-2011 06:14 PM
|
显示全部楼层
本帖最后由 jackhui 于 23-8-2011 06:18 PM 编辑
setWebViewClient 和 setWebViewClient有什么不一样吗?shouldOverrideUrlLoading 有正常操作。
jay_goh 发表于 23-8-2011 05:28 PM 
我就是不懂阿,才问你的。。
那你是怎样解决的?交流下 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 23-8-2011 06:26 PM
|
显示全部楼层
我就是不懂阿,才问你的。。
那你是怎样解决的?交流下
jackhui 发表于 23-8-2011 06:14 PM 
不好意思,因为事隔久了,忘记我怎么解决了。。不过我paste相关的code在这里,大家一起看看咯。。
- public void onReceivedError(WebView view, int errorCode,
- String description, String failingUrl) {
- // TODO Auto-generated method stub
- //Log.i("error", "error");
- //Toast.makeText(LoadHome.this, "fifth step", Toast.LENGTH_SHORT).show();
- super.onReceivedError(view, errorCode, description, failingUrl);
- webView.loadData("<html><body></body></html>", "text/html", "UTF-8");
- alertdialog = new AlertDialog.Builder(LoadHome.this).create();
- alertdialog.setMessage(Configuration.pageloaderror);
- alertdialog.setButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- System.runFinalizersOnExit(true);
- System.exit(0);
- }
- });
- alertdialog.show();
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 07:15 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-8-2011 09:13 AM
|
显示全部楼层
回复 8# win7qi
对不起哦,那是公司的project,不方便发给你们。。 |
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 10:07 AM
|
显示全部楼层
回复 win7qi
对不起哦,那是公司的project,不方便发给你们。。
jay_goh 发表于 24-8-2011 09:13 AM 
公司什么名? |
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 10:54 AM
|
显示全部楼层
公司什么名?
win7qi 发表于 24-8-2011 10:07 AM 
公司名: 麦搁啰嗦 (Microsoft) |
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 11:23 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-8-2011 11:26 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-8-2011 11:26 AM
|
显示全部楼层
回复 11# geekman
如果是的话,也不错哦。。{:2_67:} |
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 11:36 AM
|
显示全部楼层
回复 13# jay_goh
wa... 做的东西好厉害哦 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-8-2011 11:54 AM
|
显示全部楼层
回复 15# win7qi
没有啦,我也是android apps 的初学者而言。。 |
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 12:41 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 24-8-2011 01:13 PM
|
显示全部楼层
回复 jay_goh
哈哈,“麦搁啰嗦” 应该不会请人来开发 “安桌椅”(android)软件的啦。。。他们 ...
geekman 发表于 24-8-2011 12:41 PM 
芒果都出来了
|
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|