ボタンをクリックするたびに、データを保存して履歴リストに保存しようとしています。データをシリアライズおよびデシリアライズしようとするとエラーが発生します。何が間違っているのかわかりません。履歴リストに追加することはできますが、前のアクティビティに戻って戻ると、データが保持されません。この点に関する提案は役に立ちます
履歴項目:
public class HistoryItem implements Serializable {
private static final long serialVersionUID = 9056935202104448194L;
private static final String TAG = HistoryItem.class.getName();
String id;
String label;
String omxSku;
String omxProdID;
Uri uri;
transient Bitmap thumb;
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeUTF(label);
out.writeUTF(id);
out.writeUTF(omxSku);
out.writeUTF(omxProdID);
out.writeUTF(uri != null ? uri.toString() : "null");
if (thumb != null) {
// FileOutputStream thumbOut =
// HistoryItemDao.ctx.openFileOutput(getThumbFilename(),
// Context.MODE_PRIVATE);
try {
// thumb.compress(CompressFormat.PNG, 80, thumbOut);
} finally {
// thumbOut.close();
}
} else {
Log.e(TAG, "thumb is null !");
}
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
label = in.readUTF();
id = in.readUTF();
omxSku = in.readUTF();
omxProdID = in.readUTF();
}
private String getThumbFilename() throws UnsupportedEncodingException {
return URLEncoder.encode("thumb_" + id + ".png", "UTF-8");
}
}
History ItemDao
public class HistoryItemDao {
private static final String HISTORY_FILENAME = "history.ser";
private static final String TAG = HistoryItemDao.class.getName();
static Context ctx;
public HistoryItemDao(Context ctx) {
HistoryItemDao.ctx = ctx;
}
public List<HistoryItem> loadAll() {
if (!ctx.getFileStreamPath(HISTORY_FILENAME).exists()) {
return null;
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(ctx.openFileInput(HISTORY_FILENAME));
@SuppressWarnings("unchecked") // readObject() return Object.
List<HistoryItem> res = (List<HistoryItem>) in.readObject();
return res;
} catch (Exception e) {
Log.e(TAG, "Unable to deserialize history", e);
// delete it as it's likely corrupted
ctx.getFileStreamPath(HISTORY_FILENAME).delete();
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
}
}
}
public void saveAll(List<HistoryItem> items) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(ctx.openFileOutput(HISTORY_FILENAME, Context.MODE_PRIVATE));
out.writeObject(items);
} catch (Exception e) {
Log.e(TAG, "Unable to serialize history", e);
e.getMessage();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
}
}
}
}
スキャン アクティビティ
List<HistoryItem> history;
private HistoryItemDao historyItemDao;
static HistoryListAdapter historyListAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
historyItemDao=new HistoryItemDao(this);
history=historyItemDao.loadAll();
if (history == null) {
history = new ArrayList<HistoryItem>();
}
historyListAdapter = new HistoryListAdapter(this);
btnShowList = (ImageButton) findViewById(R.id.historyButton);
btnShowList.setOnClickListener(this);
click= (ImageButton) findViewById(R.id.historyButton);
click.setOnClickListener(this);
@Override
public void onClick(View v) {
if(v==click)
{
HistoryItem item = new HistoryItem();
item.id = "1";
item.label = name;
item.uri = null;
item.omxSku = omxsku2;
item.omxProdID = null;
item.thumb = null;
history.add(item);
}
else
{
Intent intent = new Intent(MainActivity.this,HistoryActivity.class);
startActivity(intent);
}
protected void onPause() {
super.onPause();
historyItemDao.saveAll(history);
}
}
ログ:
.HistoryItemDao(2100): 履歴をシリアル化できません。 java.nio.charset.ModifiedUtf8.encode(ModifiedUtf8.java:119) で .HistoryItemDao(2100): java.io.DataOutputStream.writeUTF(DataOutputStream.java:197) で .HistoryItemDao(2100): java.io.ObjectOutputStream で.writeUTF(ObjectOutputStream.java:1830) .HistoryItemDao(2100): .HistoryItem.writeObject(HistoryItem.java:100)
.HistoryItemDao(2100): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) で
.HistoryItemDao(2100): java.lang.reflect.Method.invoke(Method.java:525) で
.HistoryItemDao(2100): java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053) で
.HistoryItemDao(2100): java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) で .HistoryItemDao(2100): java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) で
.HistoryItemDao(2100): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) で .HistoryItemDao(2100): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) で .HistoryItemDao(2100): Java で.util.ArrayList.writeObject(ArrayList.java:648)
.HistoryItemDao(2100): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) で
.HistoryItemDao(2100): java.lang.reflect.Method.invoke(Method.java:525) で
.HistoryItemDao(2100): java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053) で .HistoryItemDao(2100): java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) で .HistoryItemDao(2100): Java で.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) .HistoryItemDao(2100): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) で
.HistoryItemDao(2100): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) で .HistoryItemDao(2100): com.officemax.ui.deals.HistoryItemDao.saveAll(HistoryItemDao.java:59) で .HistoryItemDao(2100) ): com.officemax.ui.deals.ScanActivity.onPause(ScanActivity.java:111) で .HistoryItemDao(2100): android.app.Activity.performPause(Activity.java:5323) で
.HistoryItemDao(2100): android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1356) .HistoryItemDao(2100): android.app.ActivityThread.performPauseActivity(ActivityThread.java:3508) .HistoryItemDao(2100): android .app.ActivityThread.performPauseActivity(ActivityThread.java:3477) .HistoryItemDao(2100): android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3454) .HistoryItemDao(2100): android.app.ActivityThread.access$800( ActivityThread.java:162)
.HistoryItemDao(2100): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) で
.HistoryItemDao(2100): android.os.Handler.dispatchMessage(Handler.java:99) で
.HistoryItemDao(2100): android.os.Looper.loop(Looper.java:158) で .HistoryItemDao(2100): android.app.ActivityThread.main(ActivityThread.java:5789) で .HistoryItemDao(2100): Java で.lang.reflect.Method.invokeNative(ネイティブ メソッド) .HistoryItemDao(2100): java.lang.reflect.Method.invoke(Method.java:525) .HistoryItemDao(2100): com.android.internal.os. ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) .HistoryItemDao(2100): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843) .HistoryItemDao(2100): dalvik.system.NativeStart. main(ネイティブメソッド)