不能在不同方法中定义的内部类中引用非最终变量名

时间:2013-03-14 08:11:18

标签: java android compiler-errors

我正在尝试创建联系人列表的CSV文件。这是我为此写的代码:

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;
}

现在我收到此错误,我在run()方法中传递了姓名和电话:

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

请建议如何解决此问题。

2 个答案:

答案 0 :(得分:1)

doInBackground已在新任务中运行。您可以使用新的thread删除代码包装。

摆脱:

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

答案 1 :(得分:1)

在Java中,

在外部类的不同方法中定义的内部类中使用的变量应该是final。

声明在run方法中使用的名称,电话,文件名为final。