Hello I'm trying to get an list of classes to be saved so when the app is closed and reopened the content will still be there. I tried to save the array to another java page but I keep getting this error.
//Main Java
builder.setPositiveButton("Add Class", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String ClassesAdded = ClassET.getText().toString();
adapter.add(ClassesAdded);
ClassET.setText("");
**FileHelper.writeData(classarray, this);//line giving me error**
}
});
//FileHelper
public class FileHelper {
public static final String FILENAME = "listinfo.dat";
public static void writeData(ArrayList<String> classarray, Context context){
try {
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(classarray);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
That error is because this
refers to the OnClickListener
in that scope but the function expects a Context. If your activity (wherever the builder call is) is named MainActivity
you would need to use MainActivity.this
there to use the right class instance, like this:
FileHelper.writeData(classarray, MainActivity.this);
Obviously replace MainActivity
with your actual class name if that's not what it's called...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.