如果没有互联网连接,AlertDialog Box无效

时间:2016-01-06 10:43:57

标签: android android-alertdialog

我在做一个谷歌地图项目。我的要求是,如果我按下当前活动中的按钮,它将进行谷歌地图活动,否则它应显示警告对话框,显示网络不可用消息。如果没有网络,应用程序崩溃。以下是代码:

public class HomeFragment extends Fragment {
          protected static final Context Context = null;

    // flag for Internet connection status
    Boolean isInternetPresent = false;

    // Connection detector class
    ConnectionDetector cd;      public HomeFragment(){}

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

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }

    @Override   public void onActivityCreated(Bundle savedInstanceState) {      // TODO Auto-generated method stub      super.onActivityCreated(savedInstanceState);

        // creating connection detector class instance
        cd = new ConnectionDetector( getActivity());


        button = (ImageButton) getActivity().findViewById(R.id.button);
                button.setOnClickListener(new OnClickListener() {
                        @Override           public void onClick(View v) {
                // TODO Auto-generated method stub


                // get Internet status
                isInternetPresent = cd.isConnectingToInternet();


                // check for Internet status
                if (isInternetPresent) {
                    // Internet Connection is Present
                    // make HTTP requests

                    Intent i = new Intent(getActivity(), GoogleActivity.class);
                    startActivity(i);       


                } else {
                    // Internet connection is not present
                    // Ask user to connect to Internet

                    showAlertDialog(Context ,  "No Internet Connection" ,  "You don't have internet connection.", true);

                }

                        }       });


            }
         public void showAlertDialog(Context HomeFragment, String title, String message, Boolean status) {
            AlertDialog alertDialog = new AlertDialog.Builder(HomeFragment).create();

            // Setting Dialog Title
            alertDialog.setTitle(title);

            // Setting Dialog Message
            alertDialog.setMessage(message);

            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

            // Setting OK Button
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                    "OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });

            // Showing Alert Message
            alertDialog.show();
        }   }

logcat的

01-06 16:22:48.940: W/dalvikvm(4223): threadid=1: thread exiting with uncaught exception (group=0x40de4540)
01-06 16:22:48.960: E/AndroidRuntime(4223): FATAL EXCEPTION: main
01-06 16:22:48.960: E/AndroidRuntime(4223): java.lang.NullPointerException
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at com.ons.bhoomi.HomeFragment.showAlertDialog(HomeFragment.java:90)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at com.ons.bhoomi.HomeFragment$1.onClick(HomeFragment.java:77)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.view.View.performClick(View.java:4102)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.view.View$PerformClick.run(View.java:17085)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.os.Handler.handleCallback(Handler.java:615)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.os.Looper.loop(Looper.java:155)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at android.app.ActivityThread.main(ActivityThread.java:5520)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at java.lang.reflect.Method.invoke(Method.java:511)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
01-06 16:22:48.960: E/AndroidRuntime(4223):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

两次更改

  1. 将AlertDialog alertDialog更改为AlertDialog.Builder alertDialog
  2. 所以它将是

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeFragment); //Here i doubt instead of HomeFragment it should be getActivity()
    
    1. 调用alertDialog.create()完成对话框设置: -

      alertDialog.create()显示();

答案 1 :(得分:0)

你的Context是null初始化它,在onActivityCreated

中添加这段代码
Context = getActivity().getApplicationContext();

答案 2 :(得分:0)

试试这种方式,

public class HomeFragment extends Fragment {

    // flag for Internet connection status
    Boolean isInternetPresent = false;

    // Connection detector class
    ConnectionDetector cd;
    ImageButton button;

    public HomeFragment() {
    }

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

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        // creating connection detector class instance
        cd = new ConnectionDetector(getActivity());

        button = (ImageButton) getActivity().findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isInternetPresent = cd.isConnectingToInternet();

                // check for Internet status
                if (isInternetPresent) {
                    // Internet Connection is Present
                    // make HTTP requests

                    Intent i = new Intent(getActivity(), GoogleActivity.class);
                    startActivity(i);

                } else {
                    // Internet connection is not present
                    // Ask user to connect to Internet

                    showAlertDialog(getActivity(), "No Internet Connection", "You don't have internet connection.", true);
                }
            }
        });
    }


    public void showAlertDialog(Context HomeFragment, String title, String message, Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(HomeFragment).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting alert dialog icon
        alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                "OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

        // Showing Alert Message
        alertDialog.show();
    }
}