unable to detect Internet in android-ThrowExceptions

Exception or error:

I have a fragment that has a WebView, everything works fine but I need to add a feature where if there is no Internet the fragment should display an error message with a try again button, for some reason my try again button is not working.

here’s my code:

NewsFragment.java
public final class NewsFragment extends Fragment {

    private WebView mWebView;
    private ImageView img;
    private Button btnTryAgain;
    private LinearLayout mLinearLayout;
    private ConnectivityManager conMgr;
    private NetworkInfo networkInfo;
    .
    .
    .

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_news,container,false);
    }

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();

        //Initialization of the above declared variables
        mWebView = getView().findViewById(R.id.webView);
        img = getView().findViewById(R.id.img404);
        btnTryAgain = getView().findViewById(R.id.btnTryAgain);
        mLinearLayout = getView().findViewById(R.id.linearLayout404);

        //Checking if user is connected to the internet or not.
        conMgr = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = conMgr.getActiveNetworkInfo();

        //this is will work everytime the fragment is called or in onResume()
        //if INTERNET connected
        if (isConnected()) {
            mWebView.setWebViewClient(new WebViewClient());
            mWebView.loadUrl("https://news.google.com/");
        } else { //if INTERNET not connected
            mLinearLayout.setVisibility(View.VISIBLE);
            mWebView.setVisibility(View.GONE);
        }

        //TryAgain Button Event handling
        btnTryAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isConnected()) {
                    mLinearLayout.setVisibility(View.GONE);
                    mWebView.setVisibility(View.VISIBLE);
                    mWebView.setWebViewClient(new WebViewClient());
                    mWebView.loadUrl("https://news.google.com/");
                    Toast.makeText(getContext(), "Connected!", Toast.LENGTH_SHORT).show();
                } else { //if INTERNET not connected
                    Toast.makeText(getContext(), "No Internet :(", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    }
    public boolean isConnected(){
        if (networkInfo != null && networkInfo.isConnected())
            return true;
        else
            return false;
    }
}

Please help me I’m new to android

How to solve:

Use this permission:

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

And use this Method:

ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
        connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
    return true;
} else {
    return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *