自定义对话框抛出WindowManager $ BadTokenException

时间:2013-10-31 10:29:05

标签: android

即时尝试创建自定义对话框,但在调用show()函数时出错。我不确定我是否错过了创建对话框的一些步骤。这是代码:

public class TrackerSettingsActivity extends Activity { 
Context context;
PhoneStateBroadcastReciever prefs;
Handler handler;
SharedPreferences m_sharedPrefs;
Editor editor;

EditText timerDelay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tracker_settings);

    timerDelay = (EditText) findViewById(R.id.editText_Set_Timer);
    m_sharedPrefs = getSharedPreferences("MyPrefs", 0);
    editor = m_sharedPrefs.edit();

    Button button = (Button) findViewById(R.id.button_Confirm);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            customDialog().show();

        }
         }
private Dialog customDialog() {
    final Dialog dialog = new Dialog(getBaseContext());
    dialog.setContentView(R.layout.login_dialog);
    dialog.setTitle("Title...");

    // set the custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Android custom dialog example!");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.ic_launcher);

    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    return dialog;
}


}

login_dialog.xml:

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:src="@drawable/ic_launcher" />

 <TextView
     android:id="@+id/text"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/image"
     android:text="sss"
     android:textColor="#FFF" />

 <Button
     android:id="@+id/dialogButtonOK"
     android:layout_width="100px"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/text"
     android:text=" Ok " />

tracker_settings.xml:

<TextView
    android:id="@+id/textView_Set_Timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="68dp"
    android:text="@string/tv_settimer"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/editText_Set_Timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/textView_Set_Timer"
    android:layout_below="@+id/textView_Set_Timer"
    android:ems="10"
    android:inputType="number"
    android:textColorHint="#ffffff" />

<Button
    android:id="@+id/button_Confirm"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText_Set_Timer"
    android:onClick="click"
    android:text="@string/bn_confirm" />

我不知道为什么show()不会工作...... 如果有人有其他建议,我愿意听... 提前谢谢!

2 个答案:

答案 0 :(得分:2)

使用此选项:必须创建对话框时使用活动参考。

Actvity activity = this;
private Dialog customDialog() {
    final Dialog dialog = new Dialog(activity); // or new Dialog(YourActivity.this);
    dialog.setContentView(R.layout.login_dialog);
    ....

}

答案 1 :(得分:1)

由于没有父视图,因此无法添加自定义对话框。正如@jzafrilla所说,为你的构造函数添加一个活动引用,以便android知道要将它附加到什么。

... = new Dialog(Main.this); // or something similar

如果你这样做,那么你应该能够在父视图的顶部看到你的对话框