简体   繁体   English

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

[英]Android: Cannot refer to a non-final variable inside an inner class defined in a different method

I know this question has been asked several times before, but I upon reading many answers, I haven't found something which suits me. 我知道这个问题以前已经问过几次了,但是我在阅读了许多答案后,没有找到适合我的东西。 First, please look at the following code, which is supposed to open an AlertDialog once an image is clicked (this works perfectly). 首先,请查看下面的代码,该代码应该在单击图像后打开AlertDialog(这很正常)。 The AlertDialog hosts an EditText field - this is where the problem arises: 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();
        }

    });

Now, this bit of code is inside my onCreate method, and the variable couponAmountString is outside the onCreate method. 现在,这段代码在我的onCreate方法内部,变量couponAmountStringonCreate方法外部。 So the code executes just fine, the only problem is that the line couponAmount.setText(couponAmountString); 因此代码执行得很好,唯一的问题是,该行couponAmount.setText(couponAmountString); does not work as I want it to. 不能按我希望的那样工作。

What I am trying to do with it is that when the user puts a value in the dialog box, and closes it by pressing OK, the value gets stored in couponAmountString - so far so good. 我正在尝试做的是,当用户在对话框中输入一个值并通过按OK将其关闭时,该值将存储在couponAmountString -到目前为止,效果很好。 When the user touches the image again to re-open the dialog box, I am trying to have the EditText field pre-set with the last value they entered, hence couponAmount.setText(couponAmountString); 当用户再次触摸图像以重新打开对话框时,我尝试将EditText字段设置为他们输入的最后一个值,因此, couponAmount.setText(couponAmountString); . But this isn't working, because I have to declare the EditText field as final . 但这是行不通的,因为我必须将EditText字段声明为final Which is why I get the error Cannot refer to a non-final variable inside an inner class defined in a different method when I click on the image the second time. 这就是为什么我收到错误消息的原因,当我第二次单击图像时, Cannot refer to a non-final variable inside an inner class defined in a different method

So is there any way I can accomplish what I want? 那么,有什么办法可以实现我想要的?

Thanks! 谢谢!

Silly, silly me. 傻,傻我。 It turns out that I was re-initializing couponAmountString inside the method as you can see. 事实证明,如您所见,我正在方法中重新初始化couponAmountString。 I just had to take out String and it's working now! 我只需要取出String即可使用了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-无法在以其他方法定义的内部类中引用非最终变量测试 - Android - Cannot refer to a non-final variable test inside an inner class defined in a different method 无法在使用其他方法定义的内部类中引用非最终变量myVariable - Cannot refer to a non-final variable myVariable inside an inner class defined in a different method 不能在不同方法中定义的内部类中引用非final变量i - Cannot refer to a non-final variable i inside an inner class defined in a different method 无法在以不同方法定义的内部类中引用非最终变量 - Cannot refer to a non-final variable inside an inner class defined in different method 无法以不同方法定义的内部类中引用非最终变量LinearLayout - Cannot refer to a non-final variable LinearLayout inside an inner class defined in a different method 无法在使用其他方法定义的内部类中引用非最终变量pcurl1 - Cannot refer to a non-final variable pcurl1 inside an inner class defined in a different method 不能在不同方法中定义的内部类中引用非final变量str - 如何解决这个问题? - Cannot refer to a non-final variable str inside an inner class defined in a different method - How to fix this? 不能在不同方法中定义的内部类中引用非最终变量名 - Cannot refer to a non-final variable name inside an inner class defined in a different method 无法以其他方法定义的内部类中引用非最终变量l - Cannot refer to a non-final variable l inside an inner class defined in a different method “无法在用不同方法定义的内部类中引用非最终变量结果” - 'Cannot refer to a non-final variable result inside an inner class defined in a different method'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM