Вложенные AlertDialogs в Android Studio - PullRequest
0 голосов
/ 21 октября 2019

Я пытаюсь создать вложенный AlertDialogs и получаю эту ошибку, после нажатия да в первом диалоговом окне:

Unable to add window -- token null is not for an application

Я исследовал проблему и перешел сюда: Вложенный AlertDialog

Я действовал согласно предложенному решению, но все еще получаю эту ошибку. Я просто не понимаю, почему токен может быть нулевым, если пользователь нажал ok. Любые предложения здесь для решения этой проблемы?

У меня есть следующий код:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import java.io.File;

public class AlertActivity extends Activity {

private File content;
private long freeSize;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_alert );
    Bundle bundle = getIntent().getExtras();
    freeSize = bundle.getLong( "freeSize" );
    content = (File) bundle.get("content");

    new AlertDialog.Builder(AlertActivity.this)

            .setTitle("Trying to Install: " + content.getName())
            .setMessage("Are you sure you want to install: " +content.getName() +"?")

            // Specifying a listener allows you to take an action before dismissing the dialog.
            // The dialog is automatically dismissed when a dialog button is clicked.
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (freeSize < content.length() * 2){
                        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                        builder.setMessage("Not enough Space!")
                                .setCancelable(false)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                    }
                                });
                        AlertDialog alert = builder.create();
                        alert.show();
                    } else{
                        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                        builder.setMessage("Start installing!")
                                .setCancelable(false)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                    }
                                });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                }
            })

            // A null listener allows the button to dismiss the dialog and take no further action.
            .setNegativeButton(android.R.string.no, null)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...