2011-04-18 7 views
7

tengo la misma excepción cuando intento de hacer estallar un PopupWindow (o diálogo) de InputMethodService:¿Cómo abrir un PopupWindow o Dialog desde un servicio de método de entrada?

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

Si intento de hacer estallar un diálogo en cambio, tengo la misma excepción exacta en la misma línea exacta de ViewRoot .Java. Aquí está mi código (abreviada):

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 

y

<?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> 

He intentado muchas variaciones del código anterior, pero siempre me dan la misma excepción para PopupWindows y cuadros de diálogo. Por alguna razón, las alertas de Toast funcionan. ¿Hay alguna técnica especial para abrir una ventana emergente o un cuadro de diálogo de un servicio (específicamente un InputMethodService), en contraposición a una actividad?

Gracias de antemano,

Barry

Respuesta

8

En realidad i logró hacerlo intente esto:

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(); 
+0

Qué clase ¿Has llamado a eso? InputMethodService o KeyboardView? –

+0

InputMethodService Pruébelo y avíseme si esta es una respuesta –

+0

Funciona como usted dijo. ¡Gracias! –

Cuestiones relacionadas