如何在非 activity class 中使用 activity 方法?当我尝试使用上下文时我的程序崩溃

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

我正在尝试在非 activity class 中使用 openFileOutput 方法。当我尝试使用来自 MainActivity (this) 的上下文时,程序崩溃。

在我的 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

我有一个个性化对话框,用来调用setEvent方法。

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;
}

同样,我尝试在下一个表单的 MainActivity class 中使用 setEvent。

Events event = new Events(this, the next parameters);

但是没用。

我已经搜索了有关此问题的答案,但找不到对我有帮助的解决方案。

我找到了这个页面,但同样的问题仍然存在。

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/

当我 运行 我的程序时,它在使用上下文时崩溃。

Logcat 显示:

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

在非 activity class 中使用 activity 方法?简而言之,你不能

但是肯定有办法,你可以传入你的 activity (这通常不是一个好主意,如果你的 activity 被破坏它可能导致空指针或内存泄漏).

另一种方法是,如果您需要上下文,可以使用 ApplicationContext。

使用 ApplicationContext 而不是上下文。由于此上下文的生命周期一直保留到应用程序被销毁或完成为止。 所以 Context 只保留到 activity 被销毁。

getApplicationContext();      

我已经解决了问题。

该方法在 activity 中有效,要在非 activity class 中使用该方法,我确实使用了:

getApplicationContext();

我确实使用过它,从 MainActivity 开始通过 CellOptions class 发送上下文,在 CellOptions class 中我确实发送了相同的上下文。

主要活动:

new CellOptions(cellOptions, getApplicationContext());

CellOptions class(上下文 = 应用程序上下文):

Events event = new Events(context);

事件class:

context.openFileOutput(events.txt);

另一个问题是我使用了“/”,而我使用了“\\”