WebView in Swipable tabs Fragment

时间:2014-03-28 06:21:41

标签: android android-fragments webview

我想创建一个布局 Analytics ..它有2个标签,分别是 Maket Resturant 其中我试图在不同的选项卡中显示不同的WebView ...标签在没有WebViews的情况下工作正常。

在我的fragment_market.xml

下面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff8400" >

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

</RelativeLayout>

以下是我的marketfragment.java

package com.example.mark;

public class MarketFragment extends Fragment {
    private WebView webView;

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


        View rootView = inflater.inflate(R.layout.fragment_market, container, false);
        webView = (WebView) getView().findViewById(R.id.webViewMarket);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        return rootView;

    }
}

这是LogCat

03-28 02:40:35.707: I/BrowserProcessMain(2677): Initializing chromium process, renderers=0
03-28 02:40:37.027: W/chromium(2677): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
03-28 02:40:37.147: E/chromium(2677): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
03-28 02:40:37.147: E/chromium(2677): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
03-28 02:40:37.147: E/chromium(2677): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
03-28 02:40:37.157: E/chromium(2677): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
03-28 02:40:37.157: E/chromium(2677): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed
03-28 02:40:38.407: D/AndroidRuntime(2677): Shutting down VM
03-28 02:40:38.407: W/dalvikvm(2677): threadid=1: thread exiting with uncaught exception (group=0xb4a1bb90)
03-28 02:40:38.617: D/dalvikvm(2677): GC_FOR_ALLOC freed 116K, 23% free 5113K/6568K, paused 129ms, total 142ms
03-28 02:40:38.677: E/AndroidRuntime(2677): FATAL EXCEPTION: main
03-28 02:40:38.677: E/AndroidRuntime(2677): Process: com.example.mark, PID: 2677
03-28 02:40:38.677: E/AndroidRuntime(2677): java.lang.NullPointerException
03-28 02:40:38.677: E/AndroidRuntime(2677):     at com.example.mark.MarketFragment.onCreateView(MarketFragment.java:20)
03-28 02:40:38.677: E/AndroidRuntime(2677):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-28 02:40:38.677: E/AndroidRuntime(2677):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-28 02:40:38.677: E/AndroidRuntime(2677):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-28 02:40:38.677: E/AndroidRuntime(2677):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-28 02:40:38.677: E/AndroidRuntime(2677):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-28 02:40:38.677: E/AndroidRuntime(2677):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)

2 个答案:

答案 0 :(得分:0)

更改此

View rootView = inflater.inflate(R.layout.fragment_market, container, false);
webView = (WebView) getView().findViewById(R.id.webViewMarket);

View rootView = inflater.inflate(R.layout.fragment_market, container, false);
webView = (WebView) rootView.findViewById(R.id.webViewMarket);

getView()返回null。您需要等到片段附加到活动。

您可以在片段getView()中使用onActivityCreated

答案 1 :(得分:0)

试试这个..

更改此

webView = (WebView) getView().findViewById(R.id.webViewMarket);

到此..

webView = (WebView) rootView.findViewById(R.id.webViewMarket);