title: Logcat error and app keeps crashing with android.view.WindowManager$BadTokenException?
body: <p>I keep getting this error and I'm not sure what to do anymore, please, can you help?</p>
  <p><strong>Logcat logs</strong></p>
  <pre><code>  Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@17b6660 -- permission denied for window type 2002
              at android.view.ViewRootImpl.setView(ViewRootImpl.java:931)
              at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:387)
              at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
              at com.example.littlezero2.Shimeji.ShimejiView (Shimeji.java:182)
              at com.example.littlezero2.Shimeji.pause (Shimeji.java:111)
              at com.example.littlezero2.Shimeji.onCreate (Shimeji.java:95)
              at android.app.ActivityThread.handleCreateService(ActivityThread.java:3953)
              at android.app.ActivityThread.access$1500(ActivityThread.java:219) 
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
              at android.os.Handler.dispatchMessage(Handler.java:107) 
              at android.os.Looper.loop(Looper.java:214) 
              at android.app.ActivityThread.main(ActivityThread.java:7356) 
              at java.lang.reflect.Method.invoke(Native Method) 
              at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
  </code></pre>
  <blockquote>
  <p>Okay, so this is a snippet of where the crash is happening I hope this
  helps. I'm not getting any errors in the build and I have already run
  it through the debugger, I've changed the phone part to WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY but still the same error occurs from Logcat. I have traced the error from the three lines given in the logcat.</p>
  </blockquote>
  <pre><code>Line 182  wm.addView ( view, params );
  Line 111  ShimejiView ();
  Line 95 pause ();
  </code></pre>
  <p>I have an error?</p>
  <blockquote>
  <p>The code where it's coming from.</p>
  <p>Java</p>
  </blockquote>
  <pre><code>@Override
          public IBinder onBind(Intent intent) {
              return mBinder;
          }
          private final IBinder mBinder = new LocalBinder();
      
          public class LocalBinder extends Binder {
              Shimeji getService() {
                  return Shimeji.this;
              }
          }
      
      
          /** end of binder */
          public void onCreate() {
              super.onCreate();
              Notification();
              pause();
          }
      
      
      
          public void onDestroy(){
              super.onDestroy();
              wm.removeViewImmediate(view);
              nm.cancel(001200);
              if (!isMuted){
                  stopsound();
              }
              timing.cancel();
              running.cancel();
          }
      
          protected void pause(){
              if (isPaused) {
                  ShimejiView();
                  randomsens();
                  handler.post(draw());
                  sp = MediaPlayer.create(getApplicationContext(), R.raw.start);
                  mp = MediaPlayer.create(getApplicationContext(), R.raw.poi);
                  bp = MediaPlayer.create(getApplicationContext(), R.raw.baka);
                  sp.start();
                  isPaused = false;
                  view.setOnTouchListener(new action());
      
                  builder.setContentText(&quot;Poi is running&quot;);
                  nm.notify(
                          01200,
                          builder.build());
              } else if(!isPaused){
                  wm.removeViewImmediate(view);
                  if (!isMuted){
                      stopsound();
                  }
                  timing.cancel();
                  running.cancel();
                  isPaused = true;
                  builder.setContentText(&quot;Poi is paused&quot;);
                  nm.notify(
                          01200,
                          builder.build());
              }
          }
      
      
          private class action implements View.OnTouchListener {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  switch (event.getAction()){
                      case MotionEvent.ACTION_DOWN:
                          idleanimation.stop();
                          view.setBackgroundResource(R.drawable.blink1);
                          blinkanimation = (AnimationDrawable)view.getBackground();
                          blinkanimation.start();
                          return true;
                      case MotionEvent.ACTION_MOVE:
                          int x_cord = (int) event.getRawX();
                          int y_cord = (int) event.getRawY();
                          params.x= (x_cord-(width/2));
                          params.y =(y_cord-(height/2)-50);
                          wm.updateViewLayout(view, params);
                          break;
                      case MotionEvent.ACTION_UP:
                          touchcheck();
                          view.setBackgroundResource(R.drawable.idle1);
                          idleanimation = (AnimationDrawable)view.getBackground();
                          idleanimation.start();
                          break;
                      case MotionEvent.ACTION_OUTSIDE:
                  }
                  return false;
              }
          }
          private void ShimejiView()
          {
              view = new ImageView(this);
              view.setBackgroundResource(R.drawable.idle1);
              idleanimation = (AnimationDrawable)view.getBackground();
              idleanimation.start();
              view.setOnTouchListener(new action());
              li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
              wm = (WindowManager) getSystemService(WINDOW_SERVICE);
              params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                      WindowManager.LayoutParams.TYPE_PHONE,
                      WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                      PixelFormat.TRANSLUCENT);
              params.gravity = Gravity.NO_GRAVITY;
              myview = li.inflate(R.layout.playground, null);
              wm.addView(view,params);
              Display display = wm.getDefaultDisplay();
              Point size = new Point();
              display.getSize(size);
              width = size.x;
              height = size.y;
          }
  </code></pre>