我正在研究android应用程序,我想在我的imageView点击监听器上创建一个选择对话框。没有对话框它工作正常,但当我添加带有列表的对话框时,它显示异常。我的代码和错误堆栈如下:
profileImage.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ImageClicked",
Toast.LENGTH_SHORT).show();
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Pick a color");
builder.setCancelable(false);
// builder.setPositiveButton(R.string.dialog_action_dismiss, null);
// creating a single choice item menu (radio buttons list)
// -1 indicates that no item should be selected by default
// pass index argument starting from 0 to preselect an item if required
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
11-25 20:03:02.625: E/AndroidRuntime(17343): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-25 20:03:02.625: E/AndroidRuntime(17343): at android.view.ViewRootImpl.setView(ViewRootImpl.java:650)
11-25 20:03:02.625: E/AndroidRuntime(17343): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
11-25 20:03:02.625: E/AndroidRuntime(17343): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-25 20:03:02.625: E/AndroidRuntime(17343): at android.app.Dialog.show(Dialog.java:281)
答案 0 :(得分:1)
将getApplicationContext()
更改为v.getContext()