2012-11-23 55 views
2

爲什麼我在Eclipse下面的代碼中發生錯誤?如何從InputMethodService打開ProgressDialog

代碼:

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) 

什麼是打開窗口progressDialog時softKeyboard(InputMethodService)開始最好的解決辦法?我嘗試「新的ProgressDialog(this);」,「新的ProgressDialog(getApplicationContext());」而是「新的ProgressDialog(ServiceKeyboard.this);」,但失敗。

+0

您需要的活動通過在你的progressDialog – njzk2

+0

的instanciation一個方面,我嘗試 「新的ProgressDialog(本);」,「新ProgressDialog (getApplicationContext());「而是「新的ProgressDialog(ServiceKeyboard.this);」,但失敗。 –

+0

是的。因爲你需要一個活動,而不是一個InputMethodService實例。你有手頭的活動嗎? – njzk2

回答

0

嘗試從Activity中調用ProgressDialog。它會解決你的問題。給我更多的解釋,你想要做什麼。

+0

應用沒有活動。應用程序是SoftKeyboard在自己的InputMethodService上。我可以在沒有任何活動的情況下從InputMethodService生成ProgressDialog嗎? –

+0

哦,讓我試試吧,爲您提供一個解決方案.. – Guna

+0

不能從服務做...相反,你可以調用一個活動..這將解決您的問題... – Guna

1

當創建一個沒有關聯Activity的InputMethodService時,我遇到了類似的問題。

這隻適用於API版本12和更高版本,但我設法通過傳入輸入視圖使用的窗口令牌來顯示ProgressDialog。只要發生這種情況,可以使用應用程序上下文構建ProgressDialog。

這需要API版本12的原因是,當輸入視圖被附接到一個窗口InputMethodService必須按順序執行OnAttachStateChangeListener通知;試圖在早期執行該代碼會產生一個空令牌。在生命週期的後期,可能會有另一個地方放置令牌請求,但是由於我的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