I'm trying to create a custom dialog and I'm having some trouble.
The custom dialog is to be shown after the user press the menu button and pick an option.
Here is the code:
@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);
}
And R.layout.email
is this:
<?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>
I'm getting this error:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
What am I doing wrong?
You should not use getApplicationContext. use
Context mContext = [ActivityName].this;