0

The following code get the error "Cannot refer to a non-final variable myid inside an inner class defined in a different method", why? Thanks!

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();        
}
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

2

because myid is not declared as final. Change

DeleteSingleSMS(long myid)

to

DeleteSingleSMS(final long myid)
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • blackbelt could you take a look at this question if you have time http://stackoverflow.com/questions/17388373/get-text-from-textview-w-button-in-custom-listview/17388422?noredirect=1#comment25243990_17388422. Is it the right way coz i am not sure about it the second part of my answer – Raghunandan Jun 30 '13 at 09:19