2017-03-02 56 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 并复制视频中的说明。之后,我添加了我的上面的代码,它完美地工作。

我最初用于选项卡式活动的教程一定是错误的。