1

QuickAction のボタンが押されたときに AlertDialog を表示しようとしています。しかし、このタスクを実行しようとすると、アプリケーションがクラッシュします。エラーの原因がわかりません。また、Thread.sleep を使用して、QuickAction を閉じる時間を与えようとしました。 AlertDialogを正しく表示しますが、機能しませんでした。

調査を行ったところ、1 つの質問が見つかりました: QuickAction ボタンからダイアログを開始する

この質問は私のものと似ていますが、getApplicationContext() を MyClass.class に置き換えようとすると、コンストラクター 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)

ありがとうございました

4

1 に答える 1

2

AlertDialog.Builder builder = new AlertDialog.Builder(yourActivityName.this);代わりに試してくださいAlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

于 2012-07-05T16:39:29.250 に答える