1

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mrc
  • 9
  • 4

3 Answers3

1

Using activity method in non-activity class? Short story is, You cannot

But there certainly is a way for that, you can pass in your activity (which usually is not a good idea, if your activity is destroyed it can caused null pointer or memory leak).

One other way is if you need the context, you can use ApplicationContext for that.

DemiDust
  • 313
  • 1
  • 3
  • 19
  • Ok, thanks for the answer. And, if I would like use some method from an activity, need I extends as activity even though some activity else is open ? – Mrc Jan 03 '19 at 21:37
  • Yes you need to if you want to call that method inseide the class – DemiDust Jan 04 '19 at 01:46
0

Use ApplicationContext instead of context. Since the lifecycle of this Context is preserved until the app is destroyed or finished. So Context is preserved only until the activity is destroyed.

getApplicationContext();      
rohiththammaiah
  • 153
  • 1
  • 5
  • When a dialog is open, the activity keeps? `getApplicationContext();` for this instruction should I use an Activity object ? – Mrc Jan 03 '19 at 21:13
0

I've achieved solve the problem.

The method works in a activity, to use the method in a non-activity class, I did use:

getApplicationContext();

I did use it, sending the context since MainActivity, through of CellOptions class, and in CellOptions class I did send the same context.

MainActivity:

new CellOptions(cellOptions, getApplicationContext());

CellOptions class (context = application context):

Events event = new Events(context);

Events class:

context.openFileOutput(events.txt);

Other problem was that I used "/", and instead I used "\\"

Mrc
  • 9
  • 4