Question

I have an AlertDialog.Builder that fires off when a user first opens the app if they do not have a network connection. The AlertDialogh as a NeutralButton that once clicked jumps into a startActivity.

I get a leaked window error. Not sure how to fix this issue since I am not able to .dismiss() or .cancel() the AlertDialog.

Is there another way to go about displaying this alert?

This is inside onCreate

        if(isNetworkAvailable()){
        checkVersion();
    }else{
         AlertDialog.Builder warning = new AlertDialog.Builder(this);
         warning.setCancelable(false);
         warning.setTitle("Network Connection Not Established");
         warning.setMessage("Data will not be Synchronized. To have data Synchronized close the application and connect to WiFi or Cell Sevice Provider then Reopen.");
         warning.setNeutralButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            findView();
                        }
                    });
         warning.show();
    }

The checkVersion() works no issues it is the findView() that gives the error

    private void findView(){
    locationId = datasource.selectSettingsLocation();
    if(locationId != 0){
        Intent intent = new Intent(Startup.this, Return.class);
        startActivity(intent);
        this.finish();
    } else {
        Intent intent = new Intent(Startup.this, Main.class);
        startActivity(intent);
        this.finish();
    }
}

The else is what is being triggered in this case Intent intent = new Intent(Startup.this, Main.class);

Was it helpful?

Solution

I get a leaked window error

You didn't posted relevant code but you need to call dismiss() on your Dialog before you're going to next Activity - in that time Context of current Activity is going to null and this is reason why you're getting this kind of Exception.

So in your case, you can do it like:

final AlertDialog.Builder warning = new AlertDialog.Builder(this);
...
public void onClick(DialogInterface arg0, int arg1) {
   warning.dismiss();
   findView();
}

Just you need to make your Dialog as final field to be able to use it in method of anonymous class.

OTHER TIPS

I guess you have the "wrong" context. Please paste your code. If you use your code in a fragment use AlterDialog.Builder builder = new AlertDialog.Builder(getActivity());

Else please post your coding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top