简体   繁体   English

可滑动选项卡中的WebView片段

[英]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. 我想创建..这有2个选项卡,即MAKETRESTURANT在我试图表现出不同的标签不同的WebView ...标签都没有网页视图完美的工作布局分析

below i my fragment_market.xml 下面我我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 下面是我的marketfragment.java

package com.example.mark; 包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 这是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. getView()返回null。 You need to wait till the fragment is attached to the activity. 您需要等到片段附加到活动上。

You can use getView() in onActivityCreated in fragment. 您可以在片段的onActivityCreated中使用getView()

Try this.. 尝试这个..

Change this 改变这个

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

to this.. 对此。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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