0

以下は、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();
        }
    }

アプリを完全にシャットダウンしてから再度開いたときに、このメモ帳アプリがランダムな新しいメモを作成する理由を解明しようとしていますが、問題が何であるかはわかりません。問題に関連していないように見えるすべての関数を切り取ったので、論理エラーはどこかにあるはずです。

ある種の循環参照またはチェックの欠如であると私が推測しているものを見つけるにはどうすればよいですか?

4

1 に答える 1

1

Android は通常、マルチバイト文字で UTF-8 を使用します。ASCII から逸脱すると、任意のバイト サブ配列で新しい文字列を作成すると、開始時と終了時に問題が発生する可能性があります。

public static Note newFromFile(NoteManager noteManager, Context context,
        String filename) throws IOException
{
    Path path = Paths.get(filename);
    byte[] bytes = Files.readAllBytes(path);
    String content = new String(bytes,  "UTF-8");

    Note n = new Note(noteManager, content.trim());
    n.fileName = filename;
    noteManager.add(n); // One registration?
    return n;
}

ノードのインスタンスが複数あるという問題は、newFromFile 内に追加するか、追加のチェックが必要になる場合があります。

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

最後に、メモを明確に定義する必要があります。

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

内容を変更できず、文字列の内容を比較できないということは、音符を重複させたり、セットを変更したり、セットの順序を混同したりできないことを意味します。

于 2016-01-30T22:30:13.677 に答える