导致创建多个备注实例的逻辑问题

时间:2016-01-30 21:33:18

标签: java android class debugging error-checking

这是一个名为note的类的(没有大多数函数)定义。

   public class Note
    {
        private String text;
        String fileName = "";
        NoteManager noteManager = null;
        List<String> hyperlinks = new ArrayList<String>();

        public static final int BUFFER_SIZE = 512;

        public Note(NoteManager noteManager) {
            this.noteManager = noteManager;
            this.text = "";
        }

        public Note(NoteManager noteManager, String content) {
            this(noteManager);
            if (content == null)
                setText("");
            else
                setText(content);
        }

        public Note(NoteManager noteManager, CharSequence content) {
            this(noteManager, content.toString());
        }

        ....some functions....

        public static Note newFromFile(NoteManager noteManager, Context context,
            String filename) throws IOException
        {

        FileInputStream inputFileStream = context.openFileInput(filename);
        StringBuilder stringBuilder = new StringBuilder();
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = inputFileStream.read(buffer)) > 0)

         {
            String line = new String(buffer, 0, len);
            stringBuilder.append(line);

            buffer = new byte[Note.BUFFER_SIZE];
        }

        Note n = new Note(noteManager, stringBuilder.toString().trim());
        n.fileName = filename;

        inputFileStream.close();

        return n;
    }

       .... some functions attributed to this class

}

这些注释由一个名为NoteManager.java的类管理,我在下面简要说明了这一点:

public class NoteManager
{
    Context context=null;
    ArrayList<Note> notes = new ArrayList<Note>();

    ..... some functions...

    public void addNote(Note note)
    {
        if (note == null || note.noteManager != this ||   notes.contains(note)) return;
        note.noteManager = this;
        notes.add(note);    
        try
        {
            note.saveToFile(context);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }


    ....some functions....

    public void loadNotes()
    {
        String[] files = context.fileList();
        notes.clear();
        for (String fname:files)
        {
            try
            {
                notes.add(Note.newFromFile(this, context, fname));
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }
}

public void addNote(Note note)
    {
        if (note == null || notes.contains(note)) return;
        note.noteManager = this;
        notes.add(note);    
        try
        {
            note.saveToFile(context);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

我想弄清楚为什么这个记事本应用程序会在应用程序完全关闭然后重新打开时创建随机新笔记,但是我无法看到问题所在。我已经删除了似乎与问题无关的所有函数,因此逻辑错误必须在某处。

如何找到我猜测是某种循环引用或缺少检查的内容?

1 个答案:

答案 0 :(得分:1)

Android通常使用带有多字节字符的UTF-8。如果偏离ASCII,在任意字节子数组上创建新String可能会在开始和结束时出现问题。

public void addNote(Note note)
{
    if (note == null || note.noteManager != this || notes.contains(note)) {
        return;
    }
    note.noteManager = this;
    notes.add(note);    

拥有多个节点实例的问题可能需要在newFromFile中添加或者可能需要额外检查:

public class Note extends Comparable<Note> {

    private NoteManager noteManager:
    private final String content; // Immutable.

    public NoteManager(NoteManager noteManager, String content) {
        this.noteManager = noteManager;
        this.content = content;
    }
    ... compare on the immutable content
    ... hashCode on content

最后必须明确注释。

IEnumerable<Match> matches

无法更改内容,并且对字符串内容进行比较,意味着注释不能加倍,更改集合,混合设置顺序。