为什么会导致错误“无法引用非最终变量”?

时间:2013-06-30 08:58:26

标签: android

以下代码获取错误“无法引用在不同方法中定义的内部类中的非最终变量myid”,为什么?谢谢!

private void DeleteSingleSMS(long myid){
    AlertDialog.Builder builder = new Builder(this);
    builder.setMessage(getString(R.string.DeleteContent));
    builder.setTitle(getString(R.string.DeleteTitle));

    builder.setPositiveButton(getString(R.string.DeleteOK), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String uri = "content://sms/" +myid;            
            getContentResolver().delete(Uri.parse(uri), null, null);    
        }
    });

    builder.setNegativeButton(getString(R.string.DeleteCancel),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });

    builder.create().show();        
}

1 个答案:

答案 0 :(得分:2)

因为myid未被声明为final。变化

DeleteSingleSMS(long myid)

DeleteSingleSMS(final long myid)