Fragment中的Webview导致崩溃

时间:2014-12-30 03:16:18

标签: android android-fragments android-webview

好的,所以我一直在学习Android Studio的应用程序。我在片段里面有一个webview。但是在应用程序中,当我在手机上打开片段时,应用程序崩溃,我的手机上写着“不幸的是,LARKer应用程序停止了。”

这是片段类:

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by Andrew on 12/29/2014.
 */
public class menu4_Fragment extends Fragment {
    private WebView mWebView;

    View rootview;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.menu4_layout,container,false);

        mWebView = (WebView) getView().findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("foo");
        mWebView.setWebViewClient(new MyWebViewClient());

        return rootview;
}

    public class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(Uri.parse(url).getHost().contentEquals("foo")) {
                // This is my website, so do not override; ler my WebView load the page

                return false;
            }

            //Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

这是XML:

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="LARK: Login"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:textSize="50dp" />

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

</RelativeLayout>

这是我手机上的图片: screenshot

3 个答案:

答案 0 :(得分:2)

更改此代码

  mWebView = (WebView) getView().findViewById(R.id.webView);

像这样,它会起作用

  mWebView = (WebView) rootview.findViewById(R.id.webView);

您必须从View Group获取视图。否则它将返回错误。

答案 1 :(得分:0)

在片段的oncreateview()方法中更改此内容

mWebView = (WebView) rootview.findViewById(R.id.webView);

答案 2 :(得分:0)

试试这段代码,

    WebView webViewInfo = (WebView) findViewById(R.id.webview);
    webViewInfo.getSettings().setJavaScriptEnabled(true);
    webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webViewInfo.setWebViewClient(new MYWEBCLIENT());
    webViewInfo.loadData("LOAD_YOUR_URL", "text/html", "UTF-8");

WebViewClient类在webview方法上实现。

private class MYWEBCLIENT extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

在键盘后部手柄上,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewInfo.canGoBack()) {
        webViewInfo.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

我希望这段代码能帮到你...... !!