0
我正在嘗試創建一個自定義對話框,並且遇到了一些麻煩。android自定義對話框錯誤
自定義對話框將在用戶按菜單按鈕並選擇一個選項後顯示。
下面是代碼:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater myMenu = getMenuInflater();
myMenu.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.email:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.email);
dialog.setTitle("Custom Dialog");
dialog.show();
break;
case R.id.info:
// Todo something
break;
}
return super.onOptionsItemSelected(item);
}
而且R.layout.email
是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/emailTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="@string/emailTitle" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" >
<requestFocus />
</MultiAutoCompleteTextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >
<TextView
android:id="@+id/emailTV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/emailBsend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/emailSend" />
<Button
android:id="@+id/emailBcancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/emailCancel" />
<TextView
android:id="@+id/emailTV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
我得到這個錯誤:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我在做什麼錯?
謝謝。回覆晚了非常抱歉。這解決了我的問題 – Favolas 2012-08-18 18:06:08