2

为什么我在下面的代码中从 Eclipse 得到错误?

代码:

public class ServiceKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener
 {
   public  static ProgressDialog progressDialog;

...
   @Override public void onStartInput( EditorInfo attribute, boolean restarting)
    {
      super.onStartInput( attribute, restarting);
      progressDialog = new ProgressDialog( ServiceKeyboard.this);
      progressDialog.setMessage( getString( R.string.sSpinner));
// after this line crash
      progressDialog.show();
...
...
      if( progressDialog != null)  progressDialog.dismiss();
    }
 }

错误:

11-23 12:53:07.854: E/AndroidRuntime(23963): FATAL EXCEPTION: main
11-23 12:53:07.854: E/AndroidRuntime(23963): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.app.Dialog.show(Dialog.java:277)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at com.avanpost.mobile.ServiceKeyboard.onStartInput(ServiceKeyboard.java:354)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.inputmethodservice.InputMethodService.doStartInput(InputMethodService.java:1549)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.inputmethodservice.InputMethodService$InputMethodImpl.startInput(InputMethodService.java:388)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:158)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.os.Looper.loop(Looper.java:137)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at java.lang.reflect.Method.invoke(Method.java:511)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 12:53:07.854: E/AndroidRuntime(23963):    at dalvik.system.NativeStart.main(Native Method)

softKeyboard(InputMethodService)启动时使用progressDialog打开窗口的最佳解决方案是什么?

我尝试“new ProgressDialog(this);”、“new ProgressDialog(getApplicationContext());” 而是“new ProgressDialog(ServiceKeyboard.this);”,但失败了。

4

2 回答 2

1

在创建没有关联 Activity 的 InputMethodService 时,我遇到了类似的问题。

这仅适用于 API 版本 12 及更高版本,但我设法通过传入输入视图使用的窗口令牌来显示 ProgressDialog。只要发生这种情况,就可以使用应用程序上下文构造 ProgressDialog。

这需要 API 版本 12 的原因是 InputMethodService 必须实现 OnAttachStateChangeListener 以便在输入视图附加到窗口时得到通知;尝试更早地执行该代码会导致 null 令牌。很可能在生命周期的后期有另一个地方可以放置令牌请求,但是由于我的 IME 已经有这些 API 要求,所以我没有费心去寻找。

编码:

public class SampleKeyboardService extends InputMethodService
                                   implements OnAttachStateChangeListener {
  private View mInputView;
  private ProgressDialog mProgress;

  @Override
  public View onCreateInputView() {
      super.onCreateInputView();
      LayoutInflater inflater =
          (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
      mInputView = inflater.inflate(R.layout.input, null);
      mInputView.addOnAttachStateChangeListener(this);
      return mInputView;
  }

  /* ... */

  @Override
  public void onViewAttachedToWindow(View view) {
      Window progressWindow = mProgress.getWindow(); // mProgress created earlier
      WindowManager.LayoutParams layoutParams = progressWindow.getAttributes();
      layoutParams.token = view.getWindowToken();
      layoutParams.type = 
          WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
      progressWindow.setAttributes(layoutParams);
  }
}

另请参阅https://stackoverflow.com/a/13962770/793212

于 2013-09-24T22:29:19.303 回答
0

尝试从 Activity 调用 ProgressDialog。它将解决您的问题。给我更多解释你想要做什么。

于 2012-11-23T13:23:14.713 回答