2011-08-30 46 views
2

對不起,又來打擾,但在我的Android鍵盤我有dialog.show()崩潰InputMethodService?

public class zyz extends InputMethodService 
implements KeyboardView.OnKeyboardActionListener { 

    private LinearLayout mInputView; 

    @Override public View onCreateInputView() { 
     mInputView = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null); 
     AddKeys("/layout.txt"); 
     return mInputView; 
    } 

... 

final Dialog dialog = new Dialog(this,R.style.myBackgroundStyle); 
...  

linLayBtn.setOnTouchListener(new View.OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 

... 
dialog.setContentView(R.layout.dialog); 
dialog.show(); 
... 

這是爲了顯示一個對話框,當按鈕被按下。然而,它的應用程序崩潰...

08-30 17:04:41.554: ERROR/AndroidRuntime(15712): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

任何想法如何解決呢?謝謝!

dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout_root" 
       android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"> 
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/frame_left" android:id="@+id/frameLeft"></ImageView> 
    <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/frame_center" android:id="@+id/LLdialog4letter"> 
     <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bun_r" android:id="@+id/ivDialogLetter" android:layout_marginTop="3px" android:layout_marginRight="9px" android:layout_marginLeft="9px"></ImageView> 
    </LinearLayout> 
    <ImageView android:layout_height="wrap_content" android:src="@drawable/frame_right" android:layout_width="wrap_content" android:id="@+id/frameRight"></ImageView> 
</LinearLayout> 
+0

顯示你如何引用上下文有關的代碼,你怎麼instnatiate對話框..... –

+0

增加,往上看。 – Roger

+0

嘗試膨脹你想在'setContentView()'設置的佈局' –

回答

2

這會爲你工作,享受

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