AsyncTask で AlertDialog を表示したいのですが、不正なトークン例外が発生します。コンテキストは、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
}
ログ猫:
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)
ご回答ありがとうございます。
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;
}
}
}