java - Android:Java 不能引用非最终变量

标签 java android android-alertdialog onclicklistener final

在我的 Android 应用程序中,当用户点击 Sales 按钮时,它会显示另一个 View 来执行销售。

如果所选客户的 GST 许可证到期日期少于 7 天并且即将到期,我会在实现警报对话框以向用户显示警告消息之前,我的应用程序正常运行,没有任何问题。现在,如果我没有为这两个变量声明 finalcustomerv,我将收到以下错误消息。

Cannot refer to a non-final variable customer inside an inner class defined in a different method

Cannot refer to a non-final variable v inside an inner class defined in a different method

我明白了

The reference declared as final cannot be modified once it is initialized.

那么,如果我将这两个变量分配为 final 会发生什么?它总是包含相同的值?有人可以向我解释为什么编译器给我这些错误以及为什么我应该为这两个变量声明 final 吗?

这是我的源代码:

private OnClickListener salesBtnClick = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Customer customer = salesCustAdpt.getSelectedCustomer();
        if (customer != null) {                                
            Date expiryDate = customer.getGSTLicenseExpiryDate();
            if (checkCustomerGSTLicenseExpiryDate(expiryDate)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SalesCustomerActivity.this);
                builder.setCancelable(false)
                    .setPositiveButton(
                        "OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    //Perform sales here!
                                    Intent intent = null;
                                    intent = new Intent(v.getContext(), SalesActivity.class);//Error msg here!
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("selCustomer", customer);//Error msg here!
                                    intent.putExtra("bundle", bundle);
                                    startActivity(intent);
                                    finish();
                                } catch (Exception ex) {
                                    AddPlusUtil.displayErrorMsg(
                                                    SalesCustomerActivity.this,
                                                    ex);
                                }
                            }
                        });

                AlertDialog alert = builder.create();
                alert.setTitle("Warning Message");
                String errMsg = "GST License Expiry Date of selected customer is \n" + expiryDate + " and going to expire soon.";
                alert.setMessage(errMsg);
                alert.setIcon(android.R.drawable.stat_notify_error);
                alert.show();
            } else {
                //Perform Sales
            }
        } 
    }
}

最佳答案

定义Customer时,使用final:

final Customer customer = salesCustAdpt.getSelectedCustomer();

此外,在您的 onClick 方法中,将 View 设置为最终 View :

public void onClick(final View v) {

希望这有帮助:)

关于java - Android:Java 不能引用非最终变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19150288/

相关文章:

java - 日期对象问题

android - 自定义 android 样式不允许整数?

java - 对话框仅在启动时第一次启动?

java - 将 spring 配置存储在类中与 xml 文件中

java - 检索 Jaxb 转换的 Enum 类中的 @XmlEnumValue 注释属性值

java - 哈希表的数组链表数组

android - 如何从另一个类中的 OnActivityResult 获取结果?(在 Activity 之外

java - 这是什么 : while ACTION_MOVE allow for a new ACTION_DOWN?

android - 在 Android 中使用缩放查看并在 Canvas 上画线

java - 更改 AlertDialog 中的字体