abbanoob abbanoob - 1 month ago 17
Android Question

Why am I getting an unreachable statement in webview?

(Novice Android Dev.)

I have a fragment which includes a webview. One problem I am facing is that the webview is unreachable. I have a fragment titled fragment_readings.xml along with ReadingsFragment.java. Ideally, I'm attempting to launch said webview when fragment is launched.

package org.coc.stthomas.stthomascoc;

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.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


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

public WebView webView;

public ReadingsFragment() {



}

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

webView = (WebView) webView.findViewById(R.id.webview);
webView.loadUrl("google.com");

// Enable Javascipt
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

//Open links in webview
webView.setWebViewClient(new WebViewClient());
return inflater.inflate(R.layout.fragment_schedule, container, false);



}


}

Answer

You have 2 return statement.

//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_readings, container, false);

All the code after first return statement, is unreachable!

Just delete the first return statement:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    webView = (WebView) webView.findViewById(R.id.webview);
    webView.loadUrl("google.com");

    // Enable Javascipt
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    //Open links in webview
    webView.setWebViewClient(new WebViewClient());

    //Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_schedule, container, false);
}