2012-04-04 91 views
0

我想加载一个谷歌日历公共日历的网址在web视图中。最初它没有问题。在某些时候,我所得到的只是webview中的文本“null”。我已经尝试更改网址以及相同结果的基本网页。我也试过注释掉wbv.loadurl这一行,并得到相同的结果。我试过了代码,当没有注释掉时,这行会被调用。任何帮助,将不胜感激。下面是填充网页视图代码:Webview显示“空”作为内容

wbv = (WebView) findViewById(R.id.audioPlayer_showSchedule); 
WebSettings settings = wbv.getSettings(); 
wbv.setWebViewClient(new TwitWebViewClient()); 
settings.setDomStorageEnabled(true); 
settings.setDatabaseEnabled(true); 
settings.setJavaScriptEnabled(true); 
settings.setDefaultTextEncodingName("utf-8"); 
wbv.loadUrl("http://www.google.com/calendar/[email protected]&ctz=America/Los_Angeles&program&mode=Week&gsessionid=vzeVWSq2Wdk3eVmGOUp1bQ"); 

回答

1

使用下面的工作代码::

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必须为网页视图网页视图

+0

工作表示感谢。 – Jason 2012-04-05 02:58:38

0

它对我来说都很好看。你确定你的TwitWebViewClient正在做你期望的吗?

另外,你确定这是你想要加载的URL吗?我不禁注意到它末尾的"gsessionid=vzeVWSq2Wdk3eVmGOUp1bQ"。当你的会议过期时,这肯定不会工作吗?