Create your dialog in Android (catch in the official documentation)

    I decided to create my Dialog in andriod. I got knowledge from official documentation. But, as it turned out, there is a catch. If you follow the recommendations from the documentation Creating a Custom Dialog , then I always get an error: the

    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

    error is caused by the method: The reason is that the wrong constructor is passed to the object constructor : It is fixed quite easily. Changing to : I think this inaccuracy will be corrected in the documentation later, but be careful.

    @Override
    protected Dialog onCreateDialog(int id) {
    Context mContext = getApplicationContext();
    Dialog dialog = new Dialog(mContext);

    dialog.setContentView(R.layout.quicklog);
    dialog.setTitle("Custom Dialog");
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");

    return dialog;
    }


    Dialog

    Context mContext = getApplicationContext();
    Dialog dialog = new Dialog(mContext);


    getApplicationContext()this

    Dialog dialog = new Dialog(this);



    Also popular now: