I would like to show an AlertDialog in AsyncTask but it gives an bad token exception. The context is given by the constructor of AsyncTask.

protected void onProgressUpdate(Object... obj) {
   AlertDialog alert;
   final CharSequence[] items = {"Run Chat", "Show Position", "Show Infos"};
   AlertDialog.Builder builder = new AlertDialog.Builder(context);
   builder.setTitle("Actions");
   builder.setItems(items, new DialogInterface.OnClickListener() {
   @Override
       public void onClick(DialogInterface arg0, int arg1) {
       // TODO Auto-generated method stub

       }

    });
     builder.show(); //it's give a bad token exception
}

Log cat :

04-29 12:56:57.035: E/AndroidRuntime(1299): Uncaught handler: thread main exiting due to uncaught exception
04-29 12:56:57.045: E/AndroidRuntime(1299): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.view.ViewRoot.setView(ViewRoot.java:472)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.app.Dialog.show(Dialog.java:239)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at fr.utbm.aiechat.TCPHandlerTask.onProgressUpdate(TCPHandlerTask.java:98)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.os.Looper.loop(Looper.java:123)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at java.lang.reflect.Method.invoke(Method.java:521)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-29 12:56:57.045: E/AndroidRuntime(1299):     at dalvik.system.NativeStart.main(Native Method)

Thank's for your answers.

public class TCPHandlerTask extends AsyncTask<Void, Object, Void>
{
    private boolean done;
    private Network Net;
    private Context context;
    private ServerSocket SocketServer;
    public TCPHandlerTask(Context applicationContext) {
        // TODO Auto-generated constructor stub
        context = applicationContext;
    }

    @Override
    protected void onPreExecute()
    {
        done = false;
        Net = Network.getInstance();
        SocketServer = Net.getSocketServer();
    }

    public void stop()
    {
        done = true;
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        while(!done)
        {
            try
            {
                Socket client = SocketServer.accept();
                InputStream is = client.getInputStream();
                ObjectInputStream dis = new ObjectInputStream(is);
                int idPacket = 0;
                idPacket = dis.readInt();
                switch(idPacket) {
                    case Packets.CHANJOIN_REQUEST : 
                        System.out.println("Paquet request join");

                        publishProgress(idPacket);
                    break;
                }
                client.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }
    protected void onProgressUpdate(Object... obj)
    {
        int idProgress = (Integer) obj[0];
        switch(idProgress) {
            case Packets.CHANJOIN_REQUEST :
                AlertDialog alert;
                final CharSequence[] items = {"Lancer Chat", "Voir Position", "Voir Infos"};
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Actions");
                builder.setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub

                    }

                });
                builder.show();
                System.out.println("context4 = ");
            break;
        }
    }
}
share|improve this question
please includes logcat error log as well. – Win Myo Htet Apr 29 '12 at 12:46
What is context? – CommonsWare Apr 29 '12 at 12:48
The context is given by the main Activity, but the aSyncTask is instancied in singleton Class. – user1364017 Apr 29 '12 at 13:01
Can you paste whole class which extends AsyncTask? – vtuhtan Apr 29 '12 at 13:21

2 Answers

I think "context" is the issue that is causing the exception.(Context is not a Activity)

EDITED:

Instead of sending Context in constructor, Send your Activity which is your class name.

private Activity activity;
 public TCPHandlerTask(Activity applicationActivity) {
    // TODO Auto-generated constructor stub
    activity = applicationActivity;
} 
share|improve this answer
Yes i think too. How i can get the current activity so ? I can show the alertDialog in onProgressUpdate no ? Thank you – user1364017 Apr 29 '12 at 12:55

Try to call AlertDialog from onPreExecute or from onPostExecute.

share|improve this answer
there is a majore difference between preExecute and postExecute. they have completely different goals – thepoosh May 17 '12 at 8:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.