【发布时间】:2014-01-23 09:24:03
【问题描述】:
我是 Android 新手,我正在开发自定义警报对话框
我想在点击编辑按钮时打开另一个对话框,代码如下
if (v.getId() == R.id.edt_order) {
System.out.println(" edit buton click");
System.out.println("Click my Order");
System.out.println(" edit clickkkkkkkkkkkkkk");
LayoutInflater li = LayoutInflater.from(getApplicationContext());
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
// result.setText(userInput.getText());
System.out.println("Click ok");
// insertData(userInput.getText().toString().trim());
Toast.makeText(getApplicationContext(), "Category added", 5000).show();
// loadSpinnerData();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
但是我遇到了一个异常,我的 log cat 输出如下
01-23 14:46:57.438: D/AndroidRuntime(660): Shutting down VM
01-23 14:46:57.448: W/dalvikvm(660): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-23 14:46:57.558: D/dalvikvm(660): GC_FOR_MALLOC freed 3899 objects / 202144 bytes in 99ms
01-23 14:46:57.568: E/AndroidRuntime(660): FATAL EXCEPTION: main
01-23 14:46:57.568: E/AndroidRuntime(660): java.lang.IllegalStateException: Could not execute method of the activity
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:2072)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.View.performClick(View.java:2408)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.View$PerformClick.run(View.java:8816)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.os.Handler.handleCallback(Handler.java:587)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.os.Looper.loop(Looper.java:123)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-23 14:46:57.568: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:46:57.568: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 14:46:57.568: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-23 14:46:57.568: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-23 14:46:57.568: E/AndroidRuntime(660): at dalvik.system.NativeStart.main(Native Method)
01-23 14:46:57.568: E/AndroidRuntime(660): Caused by: java.lang.reflect.InvocationTargetException
01-23 14:46:57.568: E/AndroidRuntime(660): at com.example.demoekot.MainScreen.clickHandler(MainScreen.java:524)
01-23 14:46:57.568: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:46:57.568: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:2067)
01-23 14:46:57.568: E/AndroidRuntime(660): ... 11 more
01-23 14:46:57.568: E/AndroidRuntime(660): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.ViewRoot.setView(ViewRoot.java:509)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-23 14:46:57.568: E/AndroidRuntime(660): at android.app.Dialog.show(Dialog.java:241)
01-23 14:46:57.568: E/AndroidRuntime(660): ... 15 more
即使我的红叉按钮工作正常,我也多次使用相同的代码来显示带有 TextView 的 AlertDialog,但我没有发现代码出了什么问题。非常感谢任何帮助。提前致谢。
现在我得到了我想要的任何东西,但是编辑和保存都带有重叠。我想隐藏编辑(蓝色按钮)并使保存按钮清晰可见。
【问题讨论】:
标签: android exception android-alertdialog illegalstateexception