简体   繁体   中英

WebView in Swipable tabs Fragment

I want to create a layout Analytics ..which has 2 tabs namely Maket and Resturant in which i am trying to show different WebView in different Tabs...tabs are working perfectly without WebViews.

below i my 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>

Below is my 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;

    }
}

Here is the 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)

Change this

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

to

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

getView() returns null. You need to wait till the fragment is attached to the activity.

You can use getView() in onActivityCreated in fragment.

Try this..

Change this

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

to this..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM