0

我想当用户单击卸载按钮时,会提示一个密码对话框。此对话框仅出现一次。我正在使用以下代码:

public void run() {

        Looper.prepare();

        while (!exit) {

            // get the info from the currently running task
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(MAX_PRIORITY);
            String activityName = taskInfo.get(0).topActivity.getClassName();
            Log.d("topActivity", "CURRENT Activity ::" + activityName);

            if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
                //Toast.makeText(context, "Uninstall Clicked", Toast.LENGTH_LONG).show();
                Intent startIntent = new Intent(this.context, Alert_Dialog.class);
                startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               this.context.startActivity(startIntent);
                exit = true;
            } else if (activityName.equals("com.android.settings.ManageApplications")) {
                Toast.makeText(this.context, "Back", Toast.LENGTH_LONG).show();
                exit = true;
            }
        }
        Looper.loop();
    }//Run

我想每当用户点击 Unistall Prompt 时,下面是 onClick 中的代码,

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);   alertDialogBuilder.setView(promptsView);
        final EditText userInput = (EditText) promptsView.findViewById(R.id.edit_text);

        alertDialogBuilder
                .setCancelable(false)
                .setNegativeButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                /** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
                                String user_text = (userInput.getText()).toString();
                                /** CHECK FOR USER'S INPUT **/
                                if (user_text.equals("abc"))
                                {
                                    Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
                                    Toast.makeText(myContext,"PAssword Correct",Toast.LENGTH_LONG).show();
                                    Alert_Dialog.this.finish();
                                    //Search_Tips(user_text);

                                }
                                else{
                                    Log.d(user_text,"string is empty");
                                    String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
                                    AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
                                    builder.setTitle("Error");
                                    builder.setMessage(message);
                                    builder.setPositiveButton("Cancel", null);
                                    builder.create().show();
                                    Alert_Dialog.this.finish();

                                }
                            }
                        });
//                .setPositiveButton("Cancel",
//                        new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog,int id) {
//                                Alert_Dialog.this.finish();
//
//                            }
//
//                        }
//                );
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }
4

1 回答 1

0

一旦对话框调用dismiss(),您为对话框设置的视图也将被破坏。

在你的情况下,这条线设置了视图,

alertDialogBuilder.setView(promptsView);

但是当对话框关闭时,视图promptsView被破坏,

promptsView应该重新创建一次你使用它。

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
promptsView = new PromptsView();//new or inflate the view
//....
alertDialogBuilder.setView(promptsView);
于 2015-04-11T09:57:07.627 回答