Supreet
Supreet

Reputation: 2531

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

I am trying to create CSV file of my contact list. Here is the code I wrote for this:

protected Void doInBackground(Void... params) {
    String name = "";
    String phone = "";
    ContentResolver cr = ReadContactsActivity.this.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);

    File folder = new File(Environment.getExternalStorageDirectory()
            + File.separator + getString(R.string.app_name));
    boolean var = false;
    if (!folder.exists())
        var = folder.mkdir();

    final String filename = folder.toString() + "/" + "Test.csv";

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // objItem = new listBean();

                    // objItem.setName(name);
                    // objItem.setNumber(phone);
                    // listArray.add(objItem);
                    new Thread() {

                        public void run() {
                            try {
                                FileWriter writer = new FileWriter(filename);
                                writer.append(name);
                                writer.append(',');
                                writer.append(phone);
                                writer.append('\n');
                                // generate whatever data you want

                                writer.flush();
                                writer.close();
                            } catch (Exception e) {
                            }
                        }
                    }.start();
                }
                pCur.close();
            }
        }
    }
    return null;
}

Now I am getting this error where I have passed name and phone in the run() method:

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

Please suggest how to resolve this problem.

Upvotes: 0

Views: 486

Answers (2)

Sadeshkumar Periyasamy
Sadeshkumar Periyasamy

Reputation: 4908

In Java,

The variables used inside an inner class which is defined in the different method of the outer class should be final.

So declare name, phone, filename which are used inside the run method as final.

Upvotes: 1

Sean
Sean

Reputation: 5256

The doInBackground is already running in a new task. You can drop the wrapping of the code with a new thread.

Just get rid of :

                  new Thread() {
                    public void run() {
                        try {

Upvotes: 1

Related Questions