Home » Android » App is crashed after inserting swipe (to refresh) code

App is crashed after inserting swipe (to refresh) code

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have used webchromeclient in fragment. I want to refresh webview in internet re-connection using swipe. Other answers doesn’t match my requirement exactly. So on the basis of other idea, I have created self answer. This code doesn’t have error, but after launching app, it is crashed. I want to know following way is wrong or not. How to solve this issue.

.java
public class tabyeb extends Fragment{

    public static boolean isNetworkStatusAvialable (Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null)
        {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if(netInfos != null)
                if(netInfos.isConnected())
                    return true;
        }
        return false;

    }


    public WebView mWebView;
    SwipeRefreshLayout mySwipeRefreshLayout;


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

        View v=inflater.inflate(R.layout.tabinfo, container, false);
        mWebView = ((WebView) v.findViewById(R.id.webview));
        mySwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeContainer);
        initWebView();
        return v;
    }

    private void initWebView() {
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings settings = mWebView.getSettings();
        settings.setSaveFormData(true);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDomStorageEnabled(true);
        settings.setSupportMultipleWindows(false);
        if(isNetworkStatusAvialable(getActivity().getApplicationContext())){
            mWebView.loadUrl("http://www.phedikholamun.gov.np/tabinfo.php");
        }
        else {
            mWebView.loadUrl("file:///android_asset/error.html");
        }
        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        mWebView.reload();
                    }
                }
        );
        }

    }
Answers: