Domanda

I created a PreferenceActivity with a Custom Title. In the title I added a button. In the button click event I added:

imageClearCache.setOnClickListener(new OnClickListener() {
              public void onClick(View v) { 
....


    new AlertDialog.Builder(getApplicationContext())
                      .setTitle("Alert!!")
                      .setMessage("Text...")
                      .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) { 
                                // continue with delete
                            }
                         }).show();
    }

I get the error: ...Unable to add window -- token null is not for an application

Any idea?

Best regards

È stato utile?

Soluzione

Use the normal context (in this case your activity) provided by the view being clicked instead of the application context.

new AlertDialog.Builder(v.getContext())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top