【问题标题】:using webview in fragment fails in android在片段中使用 webview 在 android 中失败
【发布时间】:2016-12-19 14:24:43
【问题描述】:

我有一个抽屉式菜单,我的每个菜单页面都是片段。在其中一个中,我试图打开一个 web 视图,但我得到了这个:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
        at com.example.niloofar.showroom.AboutFragment.onCreateView(AboutFragment.java:29)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这是我的片段代码:

   package com.example.niloofar.showroom;

import android.app.Activity;
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 android.widget.Button;
import android.widget.EditText;


public class AboutFragment extends Fragment {


    private WebView wv1;
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v=inflater.inflate(R.layout.fragment_about, container, false);
        wv1=(WebView)getActivity().findViewById(R.id.webView);
        wv1.setWebViewClient(new MyBrowser());

               String url = "http://bding.ir";
        WebSettings webSettings = wv1.getSettings();

        wv1.getSettings().setLoadsImagesAutomatically(true);
                wv1.getSettings().setJavaScriptEnabled(true);
                wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                wv1.loadUrl(url);
        return v;

    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

这是它的布局:

<FrameLayout 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"
tools:context="com.example.niloofar.showroom.AboutFragment">

<WebView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/webView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true" />

这是我称之为片段的菜单:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    Fragment fragment=null;

    if (id == R.id.nav_home) {
        // Handle the camera action
        fragment = new ProfileFragment();
    } else if (id == R.id.nav_points) {
        fragment = new PointFragment();
    } else if (id == R.id.nav_coupons) {
        fragment = new CouponFragment();
    } else if (id == R.id.nav_about) {
        fragment = new AboutFragment();
    }
    fragmentTransaction.replace(R.id.appbar, fragment,fragment.getClass().getSimpleName());
    fragmentTransaction.commit();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

【问题讨论】:

    标签: android android-fragments webview nullpointerexception


    【解决方案1】:

    我应该这样做:

     View v=inflater.inflate(R.layout.fragment_about, container, false);
        wv1=(WebView)v.findViewById(R.id.webView);
    

    【讨论】:

      【解决方案2】:

      如果 webview 在片段中,您应该从片段视图中获取它,如下所示。

      查看 v=inflater.inflate(R.layout.fragment_about, container, false); wv1=(WebView)v.findViewById(R.id.webView);

      【讨论】:

        【解决方案3】:

        更改以下代码

        View v=inflater.inflate(R.layout.fragment_about, container, false);
            wv1=(WebView)getActivity().findViewById(R.id.webView);
        

        View v=inflater.inflate(R.layout.fragment_about, container, false);     wv1=(WebView)v.findViewById(R.id.webView);
        

        【讨论】:

          猜你喜欢
          • 2020-03-11
          • 2016-10-15
          • 2016-09-15
          • 2021-05-10
          • 1970-01-01
          • 1970-01-01
          • 2017-11-11
          • 1970-01-01
          • 2017-07-21
          相关资源
          最近更新 更多