我是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();
}
但我得到一个例外,我的日志猫输出如下
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
即使我的redcross按钮工作正常,我也使用相同的代码多次显示AlertDialog和TextView,但我没有得到代码出错的地方。任何帮助都非常感谢。提前谢谢。
现在我得到了我想要的任何东西,但编辑和保存两者都是重叠的。我想隐藏编辑(蓝色按钮)并清楚地显示保存按钮。
答案 0 :(得分:3)
如果您阅读getApplicationContext()
处的文档,则会发现只有在需要生命周期与当前上下文不同的Context
时才应使用此文档。这不适用于您的示例并将this
对象用作className类型的对象。我想在这里您可以使用
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
OR
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ClassNAme.this);
两者都应该有用。
答案 1 :(得分:2)
替换
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
与
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivityName.this);
Dialog
需要Context
引用,其窗口令牌不是null
,例如Activity
引用...
答案 2 :(得分:0)
进行以下更改
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
答案 3 :(得分:0)
您需要Context或currentActivity。然后确定它会起作用。