2011-04-18 9 views
7

Aynı durum almak: Ben yerine Dialog pop denersenizBir giriş yöntemi hizmetinden bir PopupWindow veya Dialog nasıl başlatılır? Ben InputMethodService bir PopupWindow (veya Dialog) pop çalışırken

FATAL EXCEPTION: main 
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 
    at android.view.ViewRoot.setView(ViewRoot.java:505) 
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
    at android.widget.PopupWindow.invokePopup(PopupWindow.java:828) 
    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688) 
    at mypackage.MyInputMethodService.onClick(MyInputMethodService.java:123) 
    ... 

, ben ViewRoot tam aynı çizgide aynı istisna olsun .java.

public class MyInputMethodService 
    extends InputMethodService 
    implements View.OnClickListener { 

    public void onClick(View v) { 
     // This is the handler for View.OnClickListener 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false), 100, 100, true); 
     pw.showAtLocation(mInputView, Gravity.CENTER, 0, 0); 
     // mInputView was previously created and returned by onCreateInputView() 
    } 
} // end of MyInputMethodService 

ve

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:padding="10dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:text="Test Pop-Up" 
    /> 
</LinearLayout> 

Yukarıdaki kod birçok varyasyonlar denedi ama her zaman popupwindows ve iletişim kutuları için aynı durum almak ettik: İşte (özet) benim kodudur. Bazı nedenlerden dolayı Toast uyarıları çalışıyor. Bir Faaliyetin aksine, bir Service'dan (özellikle bir InputMethodService) bir PopupWindow veya Dialog'u başlatmak için özel bir teknik var mı?

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("Make your selection"); 
       builder.setItems(items, new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog, int item) 
        { 
         // Do something with the selection 
        } 
       }); 
       AlertDialog alert = builder.create(); 
       Window window = alert.getWindow(); 
       WindowManager.LayoutParams lp = window.getAttributes(); 
       lp.token = mInputView.getWindowToken(); 
       lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; 
       window.setAttributes(lp); 
       window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
       alert.show(); 

cevap

8

Aslında ben bu deneyin başardı? InputMethodService veya KeyboardView?
+0

bunu kimden hangi sınıf dedin: önceden

sayesinde

Barry

+0

InputMethodService bunu bir deneyin ve bu yanıtın –

+0

olduğunu söyleyin. Teşekkür ederim! –