无法从QuickAction打开AlertDialog

时间:2012-07-05 15:44:55

标签: java android alertdialog android-alertdialog

当按下QuickAction中的按钮时,我试图显示AlertDialog。但是我的应用程序在尝试完成此任务时崩溃了。我不知道导致错误的原因。我还尝试使用Thread.sleep给时间QuickAction正确地进行调整并显示AlertDialog,但它没有用。

我做了研究,发现了一个问题:Starting Dialog from QuickAction Button

这个问题有点类似于我的但是当我试图用MyClass.class替换getApplicationContext()时,我得到一个错误,说构造函数AlertDialog.builder(Class)是未定义的。

以下是一段代码QuickAction和AlertDialog:

ActionItem editQItem = new ActionItem(ID_EDIT_Q, "Edit", getResources()
            .getDrawable(R.drawable.qa_edit));
    ActionItem deleteQItem = new ActionItem(ID_DELETE_Q, "Delete",
            getResources().getDrawable(R.drawable.qa_delete_answer));
    System.out.println("Blah 1");

    mQuickActionQ = new QuickAction(this);
    mQuickActionQ.addActionItem(editQItem);
    mQuickActionQ.addActionItem(deleteQItem);
    System.out.println("Blah 2");

    // setup the action item click listener
    mQuickActionQ
            .setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
                public void onItemClick(QuickAction quickAction, int pos,
                        int actionId) {
                    ActionItem actionItem = quickAction.getActionItem(pos);
                    System.out.println("Blah 3");
                    if (actionId == ID_EDIT_Q) {
//Here im getting error from here onwards when trying to display AlertDialog
                        et_question = new EditText(getApplicationContext());
                        et_tag1 = new EditText(getApplicationContext());
                        et_tag2 = new EditText(getApplicationContext());
                        et_tag3 = new EditText(getApplicationContext());
                        et_question.setText(question);
                        et_tag1.setText(q_tag1);
                        et_tag2.setText(q_tag2);
                        et_tag3.setText(q_tag3);
                        tv_question = new TextView(getApplicationContext());
                        tv_question.setText("Question:");
                        tv_question.setTextSize(15);
                        tv_tags = new TextView(getApplicationContext());
                        tv_tags.setText("Tags:");
                        tv_tags.setTextSize(15);
                        ll_edit = new LinearLayout(getApplicationContext());
                        ll_edit.setOrientation(1);
                        ll_edit.addView(tv_question);
                        ll_edit.addView(et_question);
                        ll_edit.addView(tv_tags);
                        ll_edit.addView(et_tag1);
                        ll_edit.addView(et_tag2);
                        ll_edit.addView(et_tag3);
                        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                        builder.setMessage("Edit");
                        builder.setCancelable(true);
                        builder.setView(ll_edit);
                        builder.setPositiveButton("Save Changes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        boolean Success = true;
                                        Editable ttv = et_question.getText();
                                        final String answer = new StringBuilder().append(
                                                ttv).toString();
                                        if (answer.equals("")) {
                                            Dialog mTag = new Dialog(
                                                    getApplicationContext());
                                            mTag.setTitle("One of the field is empty!");
                                            TextView tv2 = new TextView(
                                                    getApplicationContext());
                                            tv2.setText("Blank fields are not allowed.");
                                            mTag.setContentView(tv2);
                                            mTag.setCancelable(true);
                                            mTag.show();
                                        } else {
                                            String uname = qun.getText().toString();
                                            String question = qu.getText().toString();
                                            String question_time = qpo.getText().toString();
                                            String new_question = et_question.getText()
                                                    .toString();
                                            String new_tag1 = et_tag1.getText().toString();
                                            String new_tag2 = et_tag2.getText().toString();
                                            String new_tag3 = et_tag3.getText().toString();
                                            uf = new UserFunctions();
                                            HashMap<String, String> hm = new HashMap<String, String>();
                                            db = new DatabaseHandler(
                                                    getApplicationContext());
                                            db.getReadableDatabase();
                                            hm = db.getUserDetails();
                                            db.close();
                                            String username = hm.get("username");
                                            JSONObject json = uf.updateQuestion(username,
                                                    question, question_time, new_question,
                                                    new_tag1, new_tag2, new_tag3);
                                            dialog.dismiss();
                                            pd = ProgressDialog.show(ViewMyQuestion.this,
                                                    "Please wait...",
                                                    "Updating your question!", true, false);
                                            Thread thread = new Thread(ViewMyQuestion.this);
                                            thread.start();
                                            tagcombo = new_tag1 + "," + new_tag2 + ","
                                                    + new_tag3;
                                            qun.setText(username);
                                            qtag.setText(tagcombo);
                                            qu.setText(new_question);
                                        }// end of if else
                                    }
                                });

                        builder.setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                        AlertDialog alert = builder.create();
                        alert.show();
                    } else {
                        System.out.println("Blah 4");
                        Toast.makeText(getApplicationContext(),
                                actionItem.getTitle() + " selected",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

    mQuickActionQ.setOnDismissListener(new QuickAction.OnDismissListener() {
        public void onDismiss() {
            Toast.makeText(getApplicationContext(), "Ups..dismissed",
                    Toast.LENGTH_SHORT).show();
        }
    });

以下是Logcat消息:

07-05 18:01:27.158: E/AndroidRuntime(389): FATAL EXCEPTION: main
07-05 18:01:27.158: E/AndroidRuntime(389): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.view.ViewRoot.setView(ViewRoot.java:509)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.app.Dialog.show(Dialog.java:241)
07-05 18:01:27.158: E/AndroidRuntime(389):  at com.vervecoders.cuqu.ViewMyQuestion$3.onItemClick(ViewMyQuestion.java:222)
07-05 18:01:27.158: E/AndroidRuntime(389):  at com.vervecoders.cuqu.QuickAction$2.onClick(QuickAction.java:162)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.view.View.performClick(View.java:2408)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.view.View$PerformClick.run(View.java:8816)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.os.Handler.handleCallback(Handler.java:587)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.os.Looper.loop(Looper.java:123)
07-05 18:01:27.158: E/AndroidRuntime(389):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 18:01:27.158: E/AndroidRuntime(389):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 18:01:27.158: E/AndroidRuntime(389):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 18:01:27.158: E/AndroidRuntime(389):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 18:01:27.158: E/AndroidRuntime(389):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 18:01:27.158: E/AndroidRuntime(389):  at dalvik.system.NativeStart.main(Native Method)

谢谢

1 个答案:

答案 0 :(得分:2)

尝试AlertDialog.Builder builder = new AlertDialog.Builder(yourActivityName.this);而不是AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());