webview显示不正确

时间:2014-08-31 00:15:06

标签: android webview load fragment

我正在尝试在webview下打开网址。我有以下代码:

    public class FragmentWebView extends Fragment {
    TextView webview_bar_title;
    ImageButton menu;
    WebView wv;

    public FragmentWebView() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_webview, container,
                false);

        menu = (ImageButton) view.findViewById(R.id.webview_menu);
        menu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((MainActivity) getActivity()).onBackPressed();
            }
        });

        webview_bar_title = (TextView) view.findViewById(R.id.webview_share);
        webview_bar_title.setText("Share");

        wv = (WebView) view.findViewById(R.id.fragment_webview);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl("http://www.google.com");

        return view;
    }
    }

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/webview_actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/webview_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_left" />

        <TextView
            android:id="@+id/webview_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp" />
    </RelativeLayout>

    <WebView
        android:id="@+id/fragment_webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/webview_actionbar" />

</RelativeLayout>

我正在尝试在我的网页浏览中加载Google网页。但它没有工作,并给了我错误,如网站不可用。网页谷歌可能暂时关闭,或者可能已永久移动到新地址。和建议?提前致谢。

1 个答案:

答案 0 :(得分:0)

确保您在WebView中设置了WebViewClient,并在清单中声明了INTERNET权限。

设置客户端:

wv.setWebViewClient(new WebViewClient());

在清单中声明权限:

<uses-permission android:name="android.permission.INTERNET" />

这些是我在代码中看不到的唯一内容。

此外,将WebViewClient子类化为处理不同的页面事件而不是使用默认的客户端对象,这是更常见的做法。

相关问题