使用loadUrl浏览后,Android WebView / WebViewClient使用模拟器崩溃

时间:2012-04-03 16:30:25

标签: android webview emulation

我正在使用WebViewClient使用模拟器浏览网址。过了一会儿(无法准确说明原因),页面无法再加载或者视图需要永久保存,因为之前的网站非常快。 通常我在onReceivedError下看到错误-2(WebViewClient.ERROR_HOST_LOOKUP) 任何帮助/参考将不胜感激。

谢谢!

  

W / ThrottleService(91):无法找到iface rmnet0的统计信息   D / dalvikvm(171):GC_CONCURRENT释放457K,8%免费6971K / 7559K,暂停11ms + 7ms
  F / NetworkStats(91):阅读网络统计数据的问题   F / NetworkStats(91):java.lang.IllegalStateException:解析idx 1的问题   F / NetworkStats(91):在com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
  F / NetworkStats(91):at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
  F / NetworkStats(91):at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
  F / NetworkStats(91):at com.android.server.net.NetworkStatsService.performPoll(NetworkStatsService.java:799)
  F / NetworkStats(91):at com.android.server.net.NetworkStatsService.access $ 100(NetworkStatsService.java:128)
  F / NetworkStats(91):at com.android.server.net.NetworkStatsService $ 3.onReceive(NetworkStatsService.java:633)
  F / NetworkStats(91):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:728)
  F / NetworkStats(91):在android.os.Handler.handleCallback(Handler.java:605)
  F / NetworkStats(91):在android.os.Handler.dispatchMessage(Handler.java:92)
  F / NetworkStats(91):在android.os.Looper.loop(Looper.java:137)
  F / NetworkStats(91):在android.os.HandlerThread.run(HandlerThread.java:60)
  F / NetworkStats(91):引起:java.io.FileNotFoundException:/ proc / net / xt_qtaguid / stats:open failed:ENOENT(没有这样的文件或目录)
  F / NetworkStats(91):at libcore.io.IoBridge.open(IoBridge.java:406)
  F / NetworkStats(91):在java.io.FileInputStream。(FileInputStream.java:78)
  F / NetworkStats(91):at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
  F / NetworkStats(91):... 10更多
  F / NetworkStats(91):引起:libcore.io.ErrnoException:打开失败:ENOENT(没有这样的文件或目录)
  F / NetworkStats(91):at libcore.io.Posix.open(Native Method)
  F / NetworkStats(91):at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
  F / NetworkStats(91):at libcore.io.IoBridge.open(IoBridge.java:390)
  F / NetworkStats(91):... 12更多
  D / dalvikvm(91):GC_CONCURRENT释放508K,8%免费9025K / 9735K,暂停6ms + 24ms
  W / ThrottleService(91):无法找到iface rmnet0的统计信息

1 个答案:

答案 0 :(得分:0)

使用以下工作代码::

public class Android_Activity extends Activity {
private Android_Activity _activity;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);     
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            _activity = this;   
            setContentView(R.layout.main);

            mwebview=(WebView)view.findViewById(R.id.webview);
            mwebview.getSettings().setJavaScriptEnabled(true);
            mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

            if(checkInternetConnection(_activity)==true){
                if(savedInstanceState==null)
                    mwebview.loadUrl("http://abc.com");
                else
                    mwebview.restoreState(savedInstanceState);
            }
            else{
                AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
                builder.setMessage("Please check your network connection.")
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {

                           }
                       });

                AlertDialog alert = builder.create();    
                alert.show();
            }
            mwebview.setWebChromeClient(new WebChromeClient() {

                @Override
                public void onProgressChanged(WebView view, int progress) { 
                    if(mwebview.getVisibility()==View.VISIBLE)
                    {
                        _activity.setProgress(progress * 100);
                    }
                }
            });
            mwebview.setWebViewClient(new HelloWebViewClient());
        }


        //HelloWebViewClient class for webview
        private class HelloWebViewClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }
            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);

            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                view.loadUrl(url);
                return true;
            }

        }   //HelloWebViewClient-class
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // Check if the key event was the Back button and if there's history
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebview.canGoBack() ){
                mwebview.goBack();
                return true;
            }
            // If it wasn't the Back key or there's no web page history, bubble up to the default
            // system behavior (probably exit the activity)
            return super.onKeyDown(keyCode, event);
        }
        //To check whether network connection is available on device or not
            public static boolean checkInternetConnection(Activity _activity) {
                ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                    return true;
                else
                    return false;
            }//checkInternetConnection()
}

而且在你的main.xml布局中,你必须有一个带有id webview的webview

然后替换以下行并尝试

 if(savedInstanceState==null)
                    mwebview.loadUrl("http://abc.com");
                else
                    mwebview.restoreState(savedInstanceState);

with .. mwebview.loadUrl(“http://abc.com”);