我想创建一个布局 Analytics ..它有2个标签,分别是 Maket 和 Resturant 其中我试图在不同的选项卡中显示不同的WebView ...标签在没有WebViews的情况下工作正常。
在我的fragment_market.xml 下面
以下是我的marketfragment.java package com.example.mark; 这是LogCat <?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>
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;
}
}
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)
答案 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);