简体   繁体   中英

App crash when setting progressBar visibilty to gone

Hi i'm having this problem that i simply don't understand i have a tabbed activity and each fragment has a webview in it and webviews have a progress bar that shows when they are loading but when i set the progress bar visibilty to gone the app crash the AVD log shows no exceptions but when i've commented

    progressBar.setVisibility(View.GONE);

it simply doesn't crash but the progress bar is always there if anyone can tell me where is the problem i'll be so thankful,thank you this is my code

 package org.lsvbdarija.app;

    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    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;
    import android.widget.ProgressBar;

    /**
     * Created by Anas on 20/06/2016.
     */

    public class SocialFragment extends Fragment {

    public WebView mWebView;
    ProgressBar progressBar;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1: {
                    webViewGoBack();
                }
                break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (!haveNetworkConnection()) {
            getActivity().setContentView(R.layout.activity_splashscreen);
        }

        View v = inflater.inflate(R.layout.social_layout, container, false);
        mWebView = (WebView) v.findViewById(R.id.webview);
        mWebView.loadUrl("http://www.lsvbdarija.com/search/label/%D8%B9%D9%84%D9%88%D9%85");

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient(){
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                String url2="http://www.lsvbdarija.com/";
                // all links  with in ur site will be open inside the webview
                //links that start ur domain example(http://www.example.com/)
                if (url != null && url.startsWith(url2)){
                    return false;
                }
                // all links that points outside the site will be open in a normal android browser
                else  {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                }
            }
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);

              //  progressBar.setVisibility(View.GONE);
            }
        });


        mWebView.setOnKeyListener(new View.OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }
                return false;
            }


        });

        return v;

    }


    private void webViewGoBack() {
        mWebView.goBack();
    }


    private boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }



}

Is the progress bar defined in the fragment layout or the activity layout? If it is in the fragment then it should be

progressBar = (ProgressBar) v.findViewById(R.id.progressBar);

将您的 get activity().findviewbyid() 替换为 v.findviewbyid();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM