【问题标题】:Webview in a Fragment Android Studio片段 Android Studio 中的 Webview
【发布时间】:2017-11-11 08:29:48
【问题描述】:

我在 Fragment 中的 webview 有问题。当我执行应用程序时,webview 不显示 Html 页面。这是我的代码:

    public class BingMapFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private View main_view;

    private  static View v;
    private WebView webMapView;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    ListView lv;
    TraceAdapter aAdpt;
    private  FragmentChangeActivity parent;

    private OnFragmentInteractionListener mListener;

    public BingMapFragment() {
        // Required empty public constructor
         setRetainInstance(true);
    }



    // TODO: Rename and change types and number of parameters
    public static BingMapFragment newInstance(String param1, String param2) {
        BingMapFragment fragment = new BingMapFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

            webMapView = (WebView)main_view.findViewById(R.id.bingMapView);
            webMapView.setWebViewClient(new WebViewClient());

            WebSettings ws = webMapView.getSettings();
            ws.setJavaScriptEnabled(true);
            webMapView.loadUrl("file:///android_asset/prova.html");




        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        new DataSourceDeviceRealTime().getDeviceRealTime(this);
        main_view = inflater.inflate(R.layout.fragment_bing_map,container,false); 
        v=main_view;
        parent=(FragmentChangeActivity) getActivity();
        parent.setupActionBar(Constants.SCREEN_MAP);




    return inflater.inflate(R.layout.fragment_bing_map, container, false);

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    public void goBack() {
        if(GlobalData.isSalesforcePolicyActive() ||  GlobalData.isMobileRoutePolicyActive()){
            parent.switchContent(new MenuFragment(), true);
        } else {
            parent.logoutButtonClicked(); 
        }
        }
    }

我不知道问题的根源,因为我已经在另一个应用程序中使用了 webview。谢谢你的回复。

【问题讨论】:

    标签: android android-studio android-fragments webview


    【解决方案1】:

    您在 onCreate 方法中调用 loadUrl(),但 webview 在 onCreateView 中被夸大,在 onCreate 之后调用 - 所以这可能会导致一些异常。

    在您的 onCreateView 方法中,您将布局膨胀两次 - 第一次膨胀布局并找到 WebView 实例,但您返回的是新膨胀的布局。返回的布局应用于片段...

    return main_view 放在 onCreateView() 的末尾,而不是 inflater.inflate...

    webMapView = (WebView)main_view.findViewById(R.id.bingMapView); 也应该在onCreateView 中调用

    其他一些注意事项:

    • 您应该在onStartonPause 中开始加载webview 数据
    • 要附加活动,您应该使用onAttach 方法

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多