I'm trying to use openFileOutput method in a non-activity class. When I try use a context, from the MainActivity (this), the program crash.
Some form to use that method in my class ?
private Context context;
public Events(Context context) {
this.context = context;
}
public void setEvent(int year, int month, int dayNumber, int hour, int minutes, String event, String eventParameters) {
try {
OutputStreamWriter events = new OutputStreamWriter(context.openFileOutput("events.txt", Context.MODE_PRIVATE));
} catch(IOException e) {
e.printStackTrace();
} // End of try
} // End of method - setEvent
I have a personalized dialog, it is used to call the setEvent method.
public CellOptions(final Dialog dialog) {
final Events event = new Events(dialog.getContext());
final TextView newEvent = (TextView) dialog.findViewById(R.id.newEvent), eventView = (TextView) dialog.findViewById(R.id.eventView);
newEvent.setOnClickListener(new View.OnClickListener() {
public void onClick(View option) {
event.setEvent(2018, 0, 1, 0, 0, "New year", "Nothing");
eventView.setBackgroundColor(Color.rgb(0, 0, 8));
}
});
}
public boolean showed() {
return true;
}
Too, I have tried to use setEvent in MainActivity class from the next form.
Events event = new Events(this, the next parameters);
But it doesn't work.
I have searched answers about this problem, but I can't find a solution that helps me.
I found this pages, but the same problem continue.
how to call method in activity form non activity class
Getting activity from context in android
using openFileOutput() in a class. (not an activity)
http://www.sgoliver.net/blog/ficheros-en-android-i-memoria-interna/
When I run my program, it crash when it use the context.
Logcat shows this:
01-03 15:55:25.932: W/Binder(632): Caught a RuntimeException from the binder stub implementation. 01-03 15:55:25.932: W/Binder(632): java.lang.NullPointerException 01-03 15:55:25.932: W/Binder(632): at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280) 01-03 15:55:25.932: W/Binder(632): at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129) 01-03 15:55:25.932: W/Binder(632): at android.os.Binder.execTransact(Binder.java:404) 01-03 15:55:25.932: W/Binder(632): at dalvik.system.NativeStart.run(Native Method) 01-03 15:55:25.932: W/InputMethodManagerService(487): Got RemoteException sending setActive(false) notification to pid 2744 uid 10036 01-03 15:55:26.572: I/ActivityManager(487): Displayed com.android.dropcalendary/.MainActivity: +4s402ms