我想在单击检查帐户时是否缺少帐户信息来显示警报对话框。
我在Eclipse中出现错误,其中new AlertDialog.Builder(this)the constructor AlertDialog.Builder(new View OnClickListener(){}) is undefined
如果将代码添加到活动的onCreate中,则该代码可以正常工作。

checkButton.setOnClickListener(new OnClickListener() {
        public void onClick(View Arg0){
            String AccNum = null, Store = null;
            final SharedPreferences settings = getSharedPreferences(CHECK_PREFERENCES, MODE_PRIVATE);

            if (settings.contains("Account") == true){
                AccNum = (settings.getString("Account", "default"));
                Store = (settings.getString("Store", "default"));
            }
            if (AccNum.length() < 7) {
                AlertDialog alert = new AlertDialog.Builder(this).create();
                alert.setTitle("Account Information missing!");
                alert.setMessage("Enter now? ");

                alert.setButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            startActivity(new Intent(CheckOrder.this, GoToSetup.class));
                        }
                });
                alert.setButton2("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            return;
                        }
                });
                alert.show();

            }
        }
});

最佳答案

发生错误是因为this是您正在创建的OnClickListener(在对checkButton.setOnClickListener(new OnClickListener(){的调用中),而不是父Activity。如果您的Activity类是ParentActivity,请尝试以下操作:

AlertDialog alert = new AlertDialog.Builder(ParentActivity.this).create();

关于android - Android AlertDialog构造函数未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6971004/

10-12 06:18