Android - 在Fragment中显示WebView

时间:2016-05-18 17:11:24

标签: java android android-fragments webview

所以我在XML布局文件中有一个包含以下内容的片段:

<RelativeLayout 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.dwinnbrown.myapplicationv20.MainFragment">

    <!-- TODO: Update blank fragment layout -->
    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

我现在正尝试通过与片段关联的java文件将URL加载到Web视图中。但是我遇到了一个错误“无法解析findViewByID(int)”。有什么想法吗?

以下是我尝试用于片段的代码:

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.WebView;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);


        String url = "http://winn-brown.co.uk/";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);
    }



}

3 个答案:

答案 0 :(得分:1)

做这样的事

extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll 

答案 1 :(得分:1)

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);


        String url = "http://winn-brown.co.uk/";
        WebView wv = (WebView) view.findViewById(R.id.webView);
        wv.setWebViewClient(new WebViewClient());
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl(url);
    }

答案 2 :(得分:0)

下面是片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebActivity">

<WebView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/webview">
</WebView>

</RelativeLayout>

并针对片段类尝试此操作

public class MainFragment extends Fragment {

WebView webView;

public MainFragment() {

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    webView=(WebView)getActivity().findViewById(R.id.webview);
    WebSettings webSettings=webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.loadUrl("your_url");

    webView.setWebViewClient(new Mybrowser());

    if (savedInstanceState != null)
        webView.restoreState(savedInstanceState);
    else
        webView.loadUrl("your_url");
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_main, container, false);
}
}