Android AlertDialog构造函数未定义 [英] Android AlertDialog constructor is undefined

查看:118
本文介绍了Android AlertDialog构造函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to have a alert dialog show if account info is missing when clicking the check the account. I get an error in Eclipse where new AlertDialog.Builder(this) saying the constructor AlertDialog.Builder(new View OnClickListener(){}) is undefined. The code works fine if I add it to the onCreate of the activity.

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,请尝试以下操作:

The error occurs because this is the OnClickListener that you're creating (in the call to checkButton.setOnClickListener(new OnClickListener(){), not the parent Activity. If your Activity class is ParentActivity, try this:

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

这篇关于Android AlertDialog构造函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆