Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:无法引用在其他方法中定义的内部类中的非最终变量_Android_Android Edittext_Android Alertdialog_Final - Fatal编程技术网

Android:无法引用在其他方法中定义的内部类中的非最终变量

Android:无法引用在其他方法中定义的内部类中的非最终变量,android,android-edittext,android-alertdialog,final,Android,Android Edittext,Android Alertdialog,Final,我知道这个问题以前已经被问过好几次了,但我在阅读了很多答案后,还没有找到适合我的答案。首先,请看下面的代码,它应该在单击图像后打开AlertDialog(这非常有效)。AlertDialog承载一个EditText字段-这就是问题产生的原因: ImageView scissorsImage = (ImageView) findViewById(R.id.scissorsImage); scissorsImage.setOnClickListener(new View.OnClic

我知道这个问题以前已经被问过好几次了,但我在阅读了很多答案后,还没有找到适合我的答案。首先,请看下面的代码,它应该在单击图像后打开AlertDialog(这非常有效)。AlertDialog承载一个EditText字段-这就是问题产生的原因:

    ImageView scissorsImage = (ImageView) findViewById(R.id.scissorsImage);
    scissorsImage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) 
        {
            AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setTitle("Apply a Coupon");
            alert.setMessage("Enter the coupon amount (i.e. 25 for $25)");

            // Set an EditText view to get user input 
            final EditText couponAmount = new EditText(context);
            alert.setView(couponAmount);

            couponAmount.setText(couponAmountString);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String couponAmountString = couponAmount.getText().toString();
                    System.out.println(couponAmountString);
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });

            alert.show();
        }

    });
现在,这段代码位于我的
onCreate
方法内部,变量
couponAmountString
位于
onCreate
方法外部。因此代码执行得很好,唯一的问题是行
couponAmount.setText(couponAmountString)无法按我希望的方式工作

我试图用它来做的是,当用户在对话框中输入一个值,然后按OK关闭它时,该值将存储在
couponAmountString
-到目前为止还不错。当用户再次触摸图像以重新打开对话框时,我试图用他们输入的最后一个值预先设置EditText字段,因此
couponAmount.setText(couponAmountString)。但这不起作用,因为我必须将EditText字段声明为
final
。这就是为什么当我第二次单击图像时,错误
无法引用在不同方法中定义的内部类中的非最终变量

那么,有什么方法可以实现我想要的吗


谢谢

我真傻,真傻。正如您所看到的,我在方法内部重新初始化了couponAmountString。我只需要拿出
字符串
,它现在就可以工作了