0

每次单击按钮时,我都会尝试保存数据并将其存储在历史记录列表中。在尝试序列化和反序列化数据时它给了我一个错误。我不知道我做错了什么。我可以添加到历史列表中,但是当我回到上一个活动并返回时,数据不会持续存在。在这方面的任何建议都会有所帮助

历史项目:

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");

}

    }

历史项目道

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): 无法序列化历史 .HistoryItemDao(2100): java.lang.NullPointerException .HistoryItemDao(2100): 在 java.nio.charset.ModifiedUtf8.countBytes(ModifiedUtf8.java:75) .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(Native Method)

.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(Native Method)

.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(Native Method) .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。主要(本机方法)

4

1 回答 1

0

知道了!:) 默认情况下是字符串类变量null,因此如果您不初始化它们,尝试通过writeUTF它编写它会引发异常。你可以在日志中看到:

at .HistoryItem.writeObject(HistoryItem.java:100)
at java.nio.charset.ModifiedUtf8.countBytes(ModifiedUtf8.java:75) 

在 ModifiedUtf8.java (Java API) 中的 countBytes() 方法中引发异常。源可用于:

public static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
    long result = 0;
    final int length = s.length();//it throws here, because s is null!!!

因此,您应该检查是否通过 将 null 写入流writeUTF,或者使用初始化您的类变量""

于 2013-11-01T10:01:40.280 回答