0

我正在尝试从 .txt 文件中删除特定行,该文件存储在我的 android 手机上。这就是我尝试这样做的方式:

public void removeLineFrom (String filePath, String lineToRemove){
    try {
          File oldFile = new File(filePath);
          File tempFile = new File(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/temp file.txt");

          BufferedReader br = new BufferedReader(new FileReader(filePath));
          PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

          String line = null;

          //Read from the original file and write to the new
          //unless content matches data to be removed.
          while ((line = br.readLine()) != null) {

            if (!line.equals(lineToRemove)) {

              pw.write(line);
              pw.flush();
            }
          }
          pw.close();
          br.close();

          //Delete the original file
          oldFile.delete();

          //Rename the new file to the filename the original file had.
          tempFile.renameTo(recordedFiles);

        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
}

每次我这样调用这个方法:

removeLineFrom(externalStoragePath + File.separator + 
    "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", 
    recordedFilesArray.get(toDelete));

应用程序崩溃。这是 logcat 错误:

08-08 15:24:22.225: E/AndroidRuntime(6146): java.lang.IndexOutOfBoundsException: 
    Invalid index 1, size is 1

它说问题出在上面发布的行中(调用方法)。我不知道为什么索引会无效。这是 toDelete 变量:

toDelete = arg2;

如果有人需要,整个 onItemLongClick:

listView.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            deleteAlert.setTitle("Warning");
            deleteAlert.setMessage("Are you sure you want to delete this?");
            toDelete = arg2;
            deleteAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    File directory = new File (externalStoragePath + File.separator + "Android/data/com.whizzappseasyvoicenotepad/");
                    File deleteFile = new File (directory, recordedFilesArray.get(toDelete) + ".mp3");
                    deleteFile.delete();
                    Log.i("TAG", "Deleting file: " + directory + recordedFilesArray.get(toDelete) + ".mp3");

                    listAdapter.remove(listAdapter.getItem(toDelete));
                    listAdapter.notifyDataSetChanged();

                    removeLineFrom(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedFilesArray.get(toDelete));

                    Toast toast = Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT);
                    toast.show();

                    dialog.dismiss();
                }
            });

            deleteAlert.setNegativeButton("No", null);
            deleteAlert.show();
            return false;
        }

    });
4

3 回答 3

2

问题在这里:

listAdapter.remove(listAdapter.getItem(toDelete));
listAdapter.notifyDataSetChanged();

removeLineFrom(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedFilesArray.get(toDelete));

您正在从数组中删除 arg2,在您调用您的方法之前,这就是为什么当您调用您的方法时,arg2 (toDelete) 不再存在。要使其工作,只需在从 listAdapter 中删除项目之前调用您的方法 (removeLineFrom)。

于 2013-08-08T13:43:51.793 回答
0

如果数组的大小为 1 并且您的索引为 1,那么就像它说的那样超出范围。数组从索引 0 开始。

于 2013-08-08T13:31:57.577 回答
0

recordedFilesArray只有一个元素,但您试图获得第二个元素,数组索引从 0 开始。

于 2013-08-08T13:32:55.020 回答