我制作了自己的自定义类,但我无法弄清楚它有什么问题。更具体地说,我的问题是为 android 而不是 Java 创建一个类之间存在什么区别。我的意思是,根据 logCat 它不会抛出任何错误或任何东西。相反,它告诉我InputStream
读取正确。由于某种原因,我无法保存它。完全没有。它不会抛出错误或任何东西。它只是不保存给定的参数。有任何想法吗?我认为这是因为我saveData
错误地声明了该方法的参数。
package com.eai.thepicker;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
public class DataHandler extends Activity {
FileOutputStream file_out;
FileInputStream file_in;
ObjectOutputStream obj_out;
ObjectInputStream obj_in;
ArrayList<String> retrieved_data, data_out;
private boolean switch_1;
private String tag;
private String default_message;
Context context;
ArrayList<String> data_given;
public DataHandler(Context context_given){
context = context_given;
}
@SuppressWarnings("unchecked")
public ArrayList<String> retrieveData(){
tag = "DataHandler";
default_message = "Tested";
try {
file_in = context.openFileInput("array_saved");
obj_in = new ObjectInputStream(file_in);
retrieved_data = (ArrayList<String>) obj_in.readObject();
obj_in.close();
file_in.close();
switch_1 = true;
Log.d(tag, "Loaded");
} catch (FileNotFoundException e) {
Log.d(tag, "File Not Found Exception.");
} catch (IOException e) {
Log.d(tag, "IO Exception.");
} catch (ClassNotFoundException e) {
Log.d(tag, "Class Not Found Exception.");
}
if (switch_1 == false);{
retrieved_data = new ArrayList<String>();
retrieved_data.add(default_message);
}
return retrieved_data;
}
public void saveData(ArrayList<String> data_given){
try {
file_out = context.openFileOutput("array_saved", 0);
obj_out = new ObjectOutputStream(file_out);
obj_out.writeObject(data_given);
obj_out.close();
file_out.close();
Log.d(tag, "Loaded");
} catch (FileNotFoundException e) {
Log.d(tag, "File Not Found Exception.");
} catch (IOException e) {
Log.d(tag, "IO Exception.");
}
}
}