0

请看下面的代码

用于创建自定义对话框的代码。这个类是一个动作类AlertDialog。当用户单击该按钮上的“是”按钮时AlertDialog,该类将被触发。私有类 PositiveDialogBtnAction 实现 DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), databaseConnector.getStreetAddress(selectedBranch), Toast.LENGTH_LONG).show();

            Dialog dialog = new Dialog(getApplicationContext());
            dialog.setContentView(R.layout.activity_call_dialog);
            dialog.setTitle("Select a Phone Number");

            dialog.show();
        }

    }

activity_call_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CallDialog" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

CallDialog.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class CallDialog extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_dialog);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_call_dialog, menu);
        return true;
    }

}

当我尝试运行自定义对话框时,出现以下错误

02-15 11:04:02.170: E/AndroidRuntime(585): FATAL EXCEPTION: main
02-15 11:04:02.170: E/AndroidRuntime(585): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.view.ViewRoot.setView(ViewRoot.java:531)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.app.Dialog.show(Dialog.java:241)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.example.esoftcallmanager.Form$PositiveDialogBtnAction.onClick(Form.java:155)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.os.Looper.loop(Looper.java:123)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 11:04:02.170: E/AndroidRuntime(585):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 11:04:02.170: E/AndroidRuntime(585):  at java.lang.reflect.Method.invoke(Method.java:507)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 11:04:02.170: E/AndroidRuntime(585):  at dalvik.system.NativeStart.main(Native Method)
02-15 11:04:05.520: I/Process(585): Sending signal. PID: 585 SIG: 9

为什么是这样?请帮忙!

4

1 回答 1

2

采用

Dialog dialog = new Dialog(CallDialog.this);

代替

Dialog dialog = new Dialog(getApplicationContext());

因为要显示 Dialog 你需要传递 Activity Context 而不是 Application

于 2013-02-15T05:47:53.133 回答