I have a question regarding the implementation of Android network connection status

Asked 2 years ago, Updated 2 years ago, 32 views

We are implementing login membership using Android and php mysql. Currently, what I'm trying to do is to check if there is a network connection when I launch the app, and if it's not connected, I tried to implement it to display a notification dialog and exit the app, but when I put a code to confirm the connection onCreate, I'm asking you a question.

For the implementation process other than network connection, if there is an ID and password stored in the shared preference in the main activity (login), automatically log in by linking with db through php.

If there is no information stored in the shared preference, you must enter it yourself and press the login button to enable the db linkage.

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ConnectivityManager manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


        // If either wifi or mobile network is connected,
        if (wifi.isConnected() || mobile.isConnected()) {

                    setContentView(R.layout.activity_main);
        } } else {
           AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
            {
                ad.setTitle ("Network Communication Error");
                ad.setMessage ("Please check your network connection status."");
                ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
            }
        }

Error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

android php

2022-09-22 20:00

2 Answers

I corrected ad.show() because it was missing by mistake, but regardless of this, the same error appears ,,<


2022-09-22 20:00

There is no problem with the code you uploaded. Could you upload the code for the part that calls Button's setOnClickListener() corresponding to the error log? First of all, it seems to be caused by Button referring to null in the log.


2022-09-22 20:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.