简体   繁体   中英

How to use an activity method in a non - activity class ? My program crash when I try use a context

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

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.

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();      

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 "\\\\"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM