我正在尝试创建自定义对话框,但我遇到了一些麻烦。
用户按下菜单按钮并选择一个选项后,将显示自定义对话框。
以下是代码:
@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
我做错了什么?
答案 0 :(得分:1)
您不应该使用getApplicationContext。使用
Context mContext = [ActivityName].this;