开发者

Android PopupWindow wont open properly

I am trying to do the following:

Create background service When a message is received on the inputQueue from the MQTT broker Show a popup

So I have the subscription to the mqtt broker working fine. When a message arrives - an intent is started to show the popup however the following error shows:


01-06 19:26:58.412: WARN/WindowManager(989): Failed looking up window
    01-06 19:26:58.412: WARN/WindowManager(989): java.lang.IllegalArgumentException:                        Requested window null does not exist
    01-06 19:26:58.412: WARN/WindowManager(989):     at    com.android.server.WindowManagerService.windowForClientLocked(WindowManagerService.java:9408)
    01-06 19:26:58.412: WARN/WindowManager(989):     at com.android.server.WindowManagerService.addWindow(WindowManagerService.java:1934)
01-06 19:26:58.412: WARN/WindowManager(989):     at com.android.server.WindowManagerService$Session.add(WindowManagerService.java:6886)
01-06 19:26:58.412: WARN/WindowManager(989):     at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:66)
01-06 19:26:58.412: WARN/WindowManager(989):     at com.android.server.WindowManagerService$Session.onTransact(WindowManagerService.java:6858)
01-06 19:26:58.412: WARN/WindowManager(989):     at android.os.Binder.execTransact(Binder.java:288)
01-06 19:26:58.412: WARN/WindowManager(989):     at dalvik.system.NativeStart.run(Native Method)
01-06 19:26:58.412: DEBUG/AndroidRuntime(5364): Shutting down VM
01-06 19:26:58.412: WARN/dalvikvm(5364): threadid=1: thread exiting with uncaught exception (group=0x4001d878)
01-06 19:26:58.412: WARN/WindowManager(989): Attempted to add window with token that is not a window: null.  Aborting.
01-06 19:26:58.422: ERROR/AndroidRuntime(5364): FATAL EXCEPTION: main
01-06 19:26:58.422: ERROR/AndroidRuntime(5364): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andy.tabletsms.tablet/com.andy.tabletsms.work.SMSPopup}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.os.Looper.loop(Looper.java:123)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at java.lang.reflect.Method.invoke(Method.java:521)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at dalvik.system.NativeStart.main(Native Method)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.view.ViewRoot.setView(ViewRoot.java:505)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.widget.PopupW开发者_如何学Pythonindow.showAtLocation(PopupWindow.java:688)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at com.andy.tabletsms.work.SMSPopup.onCreate(SMSPopup.java:58)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-06 19:26:58.422: ERROR/AndroidRuntime(5364):     ... 11 more
01-06 19:26:58.432: WARN/ActivityManager(989):   Force finishing activity com.andy.tabletsms.tablet/com.andy.tabletsms.work.SMSPopup
01-06 19:26:58.432: WARN/ActivityManager(989):   Force finishing activity com.andy.tabletsms.tablet/.main
01-06 19:26:58.932: WARN/ActivityManager(989): Activity pause timeout for HistoryRecord{444da2b8 com.andy.tabletsms.tablet/com.andy.tabletsms.work.SMSPopup}

The queue is checked every 5 seconds and issues a start activity for the popup if there is an item as follows


 SMSPopup.msg = main.msgs.get(0);
      Intent testActivityIntent = new Intent(context.getApplicationContext(), com.andy.tabletsms.work.SMSPopup.class);
  testActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(testActivityIntent);

The SMSPopup class looks as follows:


package com.andy.tabletsms.work;


import com.andy.tabletsms.tablet.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class SMSPopup extends Activity{

 public static String msg;
 private PopupWindow pw;

 @Override
 public void onCreate(Bundle bundle){
  super.onCreate(bundle);
  Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
  LayoutInflater inflater = LayoutInflater.from(this);
  // inflate our view from the corresponding XML file
  View layout = inflater.inflate(R.layout.popup, (ViewGroup)findViewById(R.id.popup_menu_root));
  // create a 100px width and 200px height popup window
  pw = new PopupWindow(layout, 100, 200, true);
  // set actions to buttons we have in our popup
  Button button1 = (Button)layout.findViewById(R.id.popup_menu_button1);
  button1.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View vv) {
          // close the popup
          pw.dismiss();
      }
  });
  Button button2 = (Button)layout.findViewById(R.id.popup_menu_button2);
  button2.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View vv) {
       Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
      }
  });
  Button button3 = (Button)layout.findViewById(R.id.popup_menu_button3);
  button3.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View vv) {
          finish();
      }
  });
  // finally show the popup in the center of the window


  pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

 }
}


You are displaying your popup too early, it can only be shown after the window of the activity is shown. You could simply post a Runnable to display the popup.


Other posts have pointed out the issue of attempting to popup too early. However I also think you really shouldn't be doing this:

public static String msg;

and

SMSPopup.msg = main.msgs.get(0);

This is not the right way to pass data to another Activity. You should be setting the data as an Extra on the Intent, like this:

Intent testActivityIntent = new Intent(context.getApplicationContext(), com.andy.tabletsms.work.SMSPopup.class);
testActivityIntent.putExtra("com.andy.tabletsms.message", main.msgs.get(0));
testActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(testActivityIntent);

You can then retrieve the message in your target Activity:

private String msg;
...
Intent intent = getIntent();
if (intent != null){
    Bundle bundle = intent.getExtras();
    if (bundle != null){
        msg = bundle.getString("com.andy.tabletsms.message");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜