user3720306
user3720306

Reputation: 51

Webview in Fragment causes crash

Ok so I've been working on an app in Android Studio for school. I have a webview inside of a fragment. But in the app, when ever I open the fragment on my phone, the app crashes and my phone says "Unfortunately, LARKer App has stopped."

Here's the Fragment class:

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;
        }
    }
}

And here's the 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>

Here's an image of what I got on my phone: screenshot

Upvotes: 0

Views: 1519

Answers (3)

NAP-Developer
NAP-Developer

Reputation: 3796

Try this code,

    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");

And WebViewClient class implement on webview method.

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;
    }
}

On keyboard back handle,

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

I hope this code help for you...!!

Upvotes: 0

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

change this on your oncreateview() methed of your fragmanet

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

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

Change this code

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

Like this, it will work

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

You have to get the view's from your View Group.otherwise it will return error.

Upvotes: 2

Related Questions