1

My activity starts an AsyncTask that in background send to a server some date about sensor movements (this is a loop). So, every movement is classified and corrisponds to a specific action. For this reason, I need to show an image to explain the action.

Ex: action: pour -> Image of a glass

The problems is that I need to show a text with this image so I have thought about the Dialog. When I implement it, I have this error:

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.

Someone can give me an advice about how implement the Dialog? Or that's another way to do it?

Thanks

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);
}
AngelSara
  • 161
  • 1
  • 1
  • 8

0 Answers0