每次单击按钮时,我都试图保存数据并将其存储在历史记录列表中。
尝试序列化和反序列化数据时出现错误。我不知道我在做什么错。
我能够添加到历史记录列表中,但是当我返回上一个活动并返回时,数据将不持久。在这方面的任何建议都会有所帮助
历史记录项:
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 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 res = (List) 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 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);
}
}
}
}
}
ScanActivity
List 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();
}
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):at java.lang.reflect.Method.invokeNative(本机方法)
.HistoryItemDao(2100):at 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):at java.lang.reflect.Method.invokeNative(本机方法)
.HistoryItemDao(2100):at 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):at java.lang.reflect.Method.invokeNative(本机方法)
.HistoryItemDao(2100):at 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(本机方法)
您可以从错误中发布日志,它在哪一行抛出? (以及什么)
我已经在上面发布了日志...每当我进入历史记录列表时,它就会显示一个空指针异常
如果您可以告诉我们哪一行更好,因为代码中没有行号。 您正在用null值调用某些东西,否则将发生null。.那么,您知道NullPointerException是什么。 找到该行,记录为空,然后得到答案。
out.writeObject(项目); 是抛出空指针异常的行仍无法弄清为什么会发生...。
回答,接受是否可以解决您的问题:)
得到它了!:)
字符串类变量默认为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!!!
因此,您应该检查是否通过writeUTF将null写入流,或者使用""初始化类变量。
谢谢,解决了我的问题。