我的应用程序是一个绘图应用程序,允许用户使用多种不同的笔刷颜色在画布上绘画。该应用程序将绘画保存在应用程序的多个运行中,以便在程序加载时,用户可以使用“之前的”#c;循环播放不同的绘画。和' next'纽扣。我的应用程序将绘画保存为我的自定义PaintingModel类的实例,该类表示单个绘图,而该绘制又存储Path个对象的列表。
public class PaintingModel {
private List<Path> strokes;
public PaintingModel() {
strokes = new ArrayList<>();
}
public void addStroke(Path stroke) {
strokes.add(stroke);
}
public List<Path> getStrokes() {
return strokes;
}
我保存绘画的方式是作为PaintingModel对象的集合,即List。
我用作访问全局应用程序状态的方法的单例包含保存绘图列表的代码,并在新应用程序会话中从保存文件加载它们。我已经调试了保存和从文件中读取,并确信这是正常的。这是我的单身人士课程:
public class Gallery {
private static Gallery gallery = null;
// context is required to access file writing/reading methods under Context class
private Context context = null;
// holds all saved paintings
private List<PaintingModel> paintings = new ArrayList<>();
// references the currently visible painting
private PaintingModel currentPainting; // represents the current currentPainting
// convert painting objects into serializable form
private Gson serializer = new Gson();
// references the object in memory that represents the saved painting information
private File saveFile;
// index of current painting
public int paintingIndex;
// name of saved painting file
private final String FILENAME = "gallery_save_file";
// currently selected splotch color
private int currentColor = Color.BLACK;
private Gallery(Context _context) {
context = _context;
saveFile = new File(context.getFilesDir(), FILENAME);
paintingIndex = 0;
currentPainting = new PaintingModel();
paintings = readPaintingsFromFile();
paintings.add(currentPainting);
}
public static Gallery getInstance(Context _context) {
if (gallery == null)
gallery = new Gallery(_context);
return gallery;
}
public void newStroke(Path stroke) {
currentPainting.addStroke(stroke);
savePainting();
}
private void savePainting() {
FileOutputStream fos;
try {
fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
Type listType = new TypeToken<List<PaintingModel>>() {}.getType();
String serializedPaintings = serializer.toJson(paintings, listType);
fos.write(serializedPaintings.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Read from the saved file to load all user-saved paintings
* @return List data containing saved paintings from previous sessions
*/
public List<PaintingModel> readPaintingsFromFile() {
FileInputStream fis;
byte[] obs = new byte[(int) saveFile.length()]; // add enough space for n newlines
try {
if (saveFile.exists()) {
fis = context.openFileInput(FILENAME);
int bytesRead = fis.read(obs);
if (bytesRead <= 0)
Log.i("TAG", "read 0 bytes from save file");
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.i("TAG", "file not found");
}
Type listType = new TypeToken<List<PaintingModel>>() {}.getType();
String deserializedData = new String(obs);
List<PaintingModel> results = serializer.fromJson(deserializedData, listType);
return (results == null) ? new ArrayList<PaintingModel>() : results;
}
public boolean deleteSaveFile() {
return saveFile.delete();
}
public List<PaintingModel> getPaintings() {
return paintings;
}
public PaintingModel getPainting() {
return currentPainting;
}
public int getColor() {
return currentColor;
}
public void setCurrentColor(int color) {
currentColor = color;
}
这个想法是当单身人士画廊&#39;创建后,它会将之前保存的所有绘画加载到“列表绘画”中。来自保存文件,当我需要在绘图画布视图中循环绘画时,稍后访问该文件。这就是问题所在 - 当我尝试通过访问其Path对象列表重绘上一个或下一个绘画时,应用程序崩溃,我的运行控制台输出DeadObjectException,表明主机进程已经消失。
在我的视图类中,它显示了当前的可绘制画布,以及我尝试绘制下一个/上一个绘画的地方,我有方法:
public void showNextPainting() {
drawCanvas.drawColor(Color.GRAY);
List<PaintingModel> paintings = Gallery.getInstance(getContext()).getPaintings();
PaintingModel nextPainting = paintings.get(Gallery.getInstance(getContext()).paintingIndex);
drawCanvas.drawPath(new Path(nextPainting.getStrokes().get(0)), drawPaint);
invalidate();
}
我用它首先擦除画布,然后检索单身人士持有的绘画列表中的下一幅画。在这里,我只是抓住我列表中的第一幅画作作为初步测试。无论哪个绘制对象,当我尝试在PaintingModel实例中绘制单个Path时,应用程序总是崩溃。当我绘制一个新实例化的Path对象时,这个代码工作正常,其中一个简单的形状像矩形一样附加。
完整堆栈跟踪