0

我正在尝试执行以下操作:

创建后台服务 当从 MQTT 代理在 inputQueue 上收到消息时显示弹出窗口

所以我订阅了 mqtt 代理,工作正常。当消息到达时 - 开始显示弹出窗口的意图,但是显示以下错误:


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.PopupWindow.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}

队列每 5 秒检查一次,如果有如下项目,则为弹出窗口发出启动活动


 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);

SMSPopup 类如下所示:


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);

 }
}

4

2 回答 2

2

您过早显示弹出窗口,它只能在显示活动窗口后显示。您可以简单地发布一个 Runnable 来显示弹出窗口。

于 2011-01-06T19:38:55.480 回答
1

其他帖子指出了尝试过早弹出的问题。但是我也认为你真的不应该这样做:

public static String msg;

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

这不是将数据传递给另一个的正确方法Activity。您应该将数据设置为Extraon Intent,如下所示:

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);

然后,您可以在目标中检索消息Activity

private String msg;
...
Intent intent = getIntent();
if (intent != null){
    Bundle bundle = intent.getExtras();
    if (bundle != null){
        msg = bundle.getString("com.andy.tabletsms.message");
    }
}
于 2011-01-06T20:32:02.460 回答