进度条& webview片段中没有互联网连接吐司

时间:2015-09-24 18:50:46

标签: android android-fragments webview

我在植入进度条时遇到问题,并且在我的一个webview片段中没有可用的互联网连接...这是我的课程。善于指导完整的代码。当我尝试构建应用程序时给出错误

    public class CheckNetwork {


    private static final String TAG = CheckNetwork.class.getSimpleName();



    public static boolean isInternetAvailable(Context context)
    {
        NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (info == null)
        {
            Log.d(TAG, "no internet connection");
            return false;
        }
        else
        {
            if(info.isConnected())
            {
                Log.d(TAG," internet connection available...");
                return true;
            }
            else
            {
                Log.d(TAG," internet connection");
                return true;
            }

        }
    }
}


    public class OneFragment extends Fragment{

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_one, container, false);
        WebView heroespage = (WebView) rootView.findViewById(R.id.webview);

        WebSettings webSettings = heroespage.getSettings();

        if(CheckNetwork.isInternetAvailable(OneFragment.this)) //returns true if internet available
        {
            webSettings.setJavaScriptEnabled(true);
        heroespage.loadUrl("http://google.com/");

        return rootView;
    }
    else
    {
        Toast.makeText(OneFragment.this, "No Internet Connection", 1000).show();
    }
        return rootView;
    }

}

0 个答案:

没有答案