0

このコードを考えると:

public class TestSetup extends Object implements Serializable {
   // ... some data and methods
}
ArrayList SetupArray = new ArrayList<TestSetup>();
// ----------------------------------------------
    public boolean readExternalStorageObjectFile(String filename, Object obj) {
    boolean isOk = true;
    try {
        File path = new File(sDirectoryPath + sTopDirectoryObj, filename);
        FileInputStream out = new FileInputStream(path);
        ObjectInputStream o = new ObjectInputStream(out);
        obj = o.readObject();
        o.close();
    }
    catch(Exception e) {
        Log.e("Exception","Exception occured in reading");
        isOk = false;
    }        
    return isOk;
}

// ----------------------------------------------
public void loadSetups() {
    this.SetupArray.clear();
    this.readExternalStorageObjectFile(SETUPS_FILENAME, this.SetupArray);
}

loadSetups() の this.SetupArray には、readExternalStorageObjectFile() から読み込まれた既存の配列情報が含まれていると思いますが、そうではありません。

readExternalStorageObjectFile() にブレークポイントを設定すると、readObject() の実行時に obj に ArrayList 情報が含まれていることがわかります。

しかし、loadSetups() に戻ると、this.SetupArray はそうではありません。それは空です。

obj を ArrayList としてキャストしようとしましたが、同じ結果です。

4

1 に答える 1

1

パラメータはobjポインタです。参照データを変更せずに再割り当てする場合obj = o.readObject()は、ポインターを別のメモリ位置に再割り当てするだけです。

解決策は、メソッドがオブジェクトを返すようにすることです。

ArrayList SetupArray = new ArrayList<TestSetup>();

public Object readExternalStorageObjectFile(String filename) {
    try {
        File path = new File(sDirectoryPath + sTopDirectoryObj, filename);
        FileInputStream out = new FileInputStream(path);
        ObjectInputStream o = new ObjectInputStream(out);
        Object obj = o.readObject();
        o.close();
        return obj;
    }
    catch(Exception e) {
        Log.e("Exception","Exception occured in reading");
        return null;
    }        
}

public void loadSetups() {
    this.SetupArray = (ArrayList) this.readExternalStorageObjectFile(SETUPS_FILENAME);
}
于 2012-08-02T13:53:24.857 に答える