Pregunta

I want to design my application in such a way that when the app is installed in the device it should first check if the app is connected to internet or not before displaying the layout. So goin about this approach below.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    connectivity = new ConnectionDetector(getApplicationContext());
    alertDialog =  new AlertDialogManager();
    //Checking for Internet connection
    if(!connectivity.isConnectingToInternet()){
        alertDialog.showAlertDialog(getApplicationContext(), "Internet Connection Error", "Not connected to internet", false);
        return ;
    }
    else{
        setContentView(R.layout.activity_register);

        me = (EditText) findViewById(R.id.fname);
        email = (EditText) findViewById(R.id.email);
        age = (EditText) findViewById(R.id.age);
        address = (EditText) findViewById(R.id.address);
        state = (EditText) findViewById(R.id.state);
        country = (EditText) findViewById(R.id.country);
        done = (Button) findViewById(R.id.done);
    }
}

My ConnectionDector Class is as follow:

public class ConnectionDetector {

private Context context;

public ConnectionDetector(Context context){
    this.context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info = connectivity.getAllNetworkInfo();
    if(info != null){
        for(int i = 0; i< info.length; i++){
            if(info[i].getState() == NetworkInfo.State.CONNECTED){
                return true;
            }
        }
    }
    return false;
}
}

Below is my AlertDialogManager Class:

    public class AlertDialogManager {

@SuppressWarnings("deprecation")
 public void showAlertDialog(Context context, String title, String message, Boolean status){
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    if(status != null){
        alertDialog.setIcon((status)?R.drawable.success : R.drawable.fail);
    }
    alertDialog.setButton("Okay", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    alertDialog.show();
}

My app gets installed properly if the I am connected to wifi. If I am not its get crashed when I try to install it and gives the following error:

03-24 17:01:11.169: E/AndroidRuntime(6101): FATAL EXCEPTION: main
03-24 17:01:11.169: E/AndroidRuntime(6101): Process: com.iriemo.safetyapp, PID: 6101
03-24 17:01:11.169: E/AndroidRuntime(6101): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.demoApp/com.example.demoApp.RegisterActivity}: java.lang.NullPointerException
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.os.Looper.loop(Looper.java:136)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at java.lang.reflect.Method.invoke(Method.java:515)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at dalvik.system.NativeStart.main(Native Method)
03-24 17:01:11.169: E/AndroidRuntime(6101): Caused by: java.lang.NullPointerException
03-24 17:01:11.169: E/AndroidRuntime(6101):     at  RegisterActivity.onCreate(RegisterActivity.java:26)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at      android.app.Activity.performCreate(Activity.java:5231)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-24 17:01:11.169: E/AndroidRuntime(6101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-24 17:01:11.169: E/AndroidRuntime(6101):     ... 11 more
¿Fue útil?

Solución

add alertDialog = new AlertDialog(getApplicationContext()); this will solve the null pointer exception

Otros consejos

Change this.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
connectivity = new ConnectionDetector(this);

//Checking for Internet connection
if(!connectivity.isConnectingToInternet()){
    alertDialog.showAlertDialog(getApplicationContext(), "Internet Connection Error", "Not connected to internet", false);
    return ;
}
else{
   // Something else.  
}

me = (EditText) findViewById(R.id.fname);
email = (EditText) findViewById(R.id.email);
age = (EditText) findViewById(R.id.age);
address = (EditText) findViewById(R.id.address);
state = (EditText) findViewById(R.id.state);
country = (EditText) findViewById(R.id.country);
done = (Button) findViewById(R.id.done);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top