我的活动启动了一个AsyncTask,它在后台向服务器发送一些关于传感器移动的日期(这是一个循环)。
因此,每个动作都被分类并对特定动作进行约束。
出于这个原因,我需要显示一个图像来解释这个动作。
例如:行动:倒 - >玻璃的图像
问题是我需要用这个图像显示一个文本,所以我想到了Dialog。
当我实现它时,我有这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sara.myapplication, PID: 32472
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:574)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at com.example.stefano.myapplication.Sensor.showImage(MetaWearAsyncTask.java:234)
at com.example.stefano.myapplication.Sensor.onProgressUpdate(MetaWearAsyncTask.java:216)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
I/Process: Sending signal. PID: 32472 SIG: 9
Application terminated.
有人可以就如何实施Dialog给我一个建议吗?或者那是另一种方式吗?
由于
的AsyncTask:
protected void onProgressUpdate(Object[] values) {
Movement movement = (Movement) values[0];
if((movement.getAction().toString()).equals("Pour")){
showImage(R.drawable.pour);
}
else if((movement.getAction().toString()).equals("OpenDoor")){
showImage(R.drawable.open_door);
}
else if((movement.getAction().toString()).equals("Cut")){
showImage(R.drawable.knife);
}
else {
Toast.makeText(context, device + ": " +"Recognized: " + movement.getAction(), Toast.LENGTH_SHORT).show();
}
}
private void showImage(int idImage) {
dialog.setIcon(idImage);
dialog.setTitle(device);
dialog.show();
//stop dialog after 1.5 seconds
Runnable progressRunnable = new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 1500);
}