2017-03-02 65 views
0

我一直在環顧這個網站和谷歌搜索,但我恐怕我被卡住了。我只是無法讓webview在我的片段中工作。我在這裏嘗試了很多不同的代碼,但一定有一些我錯過了。Webview在片段中工作

XML

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout 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" 
 
    android:orientation="vertical" 
 
    tools:context="layout.GER1"> 
 

 
    <WebView 
 
     android:id="@+id/webview" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:layout_alignParentRight="true" 
 
     android:layout_alignParentEnd="true" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_alignParentStart="true"> 
 
    </WebView> 
 

 

 
</RelativeLayout>

的Java

package layout; 
 

 
import android.os.Bundle; 
 
import android.support.v4.app.Fragment; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 
import android.webkit.WebSettings; 
 
import android.webkit.WebView; 
 
import android.webkit.WebViewClient; 
 
import com.example.a1cmanny1.bmi.R; 
 

 
public class GER1 extends Fragment { 
 

 
    public WebView mWebView; 
 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
 
          Bundle savedInstanceState) { 
 

 
     View v=inflater.inflate(R.layout.fragment_ger1, container, false); 
 
     mWebView = (WebView) v.findViewById(R.id.webview); 
 
     mWebView.loadUrl("https://www.google.co.nz/"); 
 

 
     // Enable Javascript 
 
     WebSettings webSettings = mWebView.getSettings(); 
 
     webSettings.setJavaScriptEnabled(true); 
 

 
     // Force links and redirects to open in the WebView instead of in a browser 
 
     mWebView.setWebViewClient(new WebViewClient()); 
 

 
     return v; 
 
    } 
 
}

任何幫助,將不勝感激。

+0

代碼中的所有內容都看起來不錯。你面臨的問題是什麼? –

+0

當我運行它時,webview只是空白。它甚至不嘗試加載。可能我錯過了啓用片段中webview的活動中的某些內容? – 1cmanny1

+0

我也在使用標籤式活動 - 滑動視圖模板。我在清單文件中有。 – 1cmanny1

回答

0

我明白了。

我不知道爲什麼我的初始代碼不工作,但我發現這篇文章: Webview not working in fragment 並複製視頻中的說明。之後,我添加了我的上面的代碼,它完美地工作。

我最初用於選項卡式活動的教程一定是錯誤的。