2016-08-15 69 views
1
页面

我已经把WebView在我的片段布局:的WebView在片段不加载

<FrameLayout 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="com.deped.k12.GradeSheet" 
    android:background="#fafafa"> 

    <!-- TODO: Update blank fragment layout --> 

    <WebView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/webView" /> 

</FrameLayout> 

但是,当我尝试从它加载任何网站。我只是得到一个空白WebViewenter image description here

我试图寻找Android监视器中的错误消息,但没有。

这是我在片段使用代码加载网址:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_grade_sheet, container, false); 

     String url = "http://www.google.com/"; 
     WebView wv = (WebView)view.findViewById(R.id.webView); 
     WebSettings settings = wv.getSettings(); 
     wv.setWebChromeClient(new WebChromeClient() { 
     }); 
     final String mimeType = "text/html"; 
     final String encoding = "UTF-8"; 
     String html = ""; 
     settings.setJavaScriptEnabled(true); 
     wv.loadDataWithBaseURL(url, html, mimeType, encoding, ""); 


     return view; 
    } 

我还添加了正确的权限:

enter image description here

什么可能是这里的问题?

回答

1

下面的代码对我的作品:

WebView webView = (WebView) view.findViewById(R.id.webView); 
webView.setWebViewClient(new WebViewClient()); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url); 

我真的不知道,如果你只需要添加一个WebViewClient。

更多信息:WebViewClient vs WebChromeClient

0

您的问题是:

String html = ""; 
settings.setJavaScriptEnabled(true); 
wv.loadDataWithBaseURL(url, html, mimeType, encoding, ""); 

你加载你的空html字符串到web视图。

尝试类似:wv.loadUrl("http://example.com/");而不是更新您的html字符串。

你应该看看loadDataWithBaseURL方法的API文档。