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

これは、別の にデータを渡す正しい方法ではありませんActivityExtra次のように、データを に設定する必要があります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 に答える