如何在不使用静态上下文的情况下将上下文传递给模块类? [英] How to pass context to a module classes without using a static context?
问题描述
我创建了一个库,并作为模块添加到我的应用程序中.我的库没有任何 activity class
,所有类都是核心 java 类,所以我无权访问 context
.
这就是我所做的;
- 我的库类是
singleton
- 我创建了一个像这样的静态变量;
静态上下文 myGlobalContext
- 我创建了一个方法来设置这个上下文;
public void init(上下文上下文){myGlobalContext=上下文;}
我从第一个活动设置了这个上下文;
MyClass.sharedInstance().init(MyApplication.getAppContext());
然后我在所有库类中使用这个 myGlobalContext
.当我调试它时,这东西工作正常,myGlobalContext
的值永远不会为空,但我从 crashlytics 中遇到了一些崩溃,其中 context
为空.
java.lang.NullPointerException:尝试调用虚方法'android.content.SharedPreferencesandroid.content.Context.getSharedPreferences(java.lang.String, int)'在空对象引用上
当我运行 lint
时,它还警告 static global context
这是我的应用程序类
,我在这里创建上下文;
public class MyApplication extends Application {私有静态上下文上下文;公共无效 onCreate() {super.onCreate();MyApplication.context = getApplicationContext();}公共静态上下文 getAppContext() {返回 MyApplication.context;}
替换
MyApplication.context = getApplicationContext();
与
MyApplication.context = this;
并且您需要将应用程序名称添加到清单以便设置此上下文
但是,如果您有核心 Java 库,则不应使用应用程序,因为您没有生命周期方法要处理.只需定义使用 Context 对象的方法,从外部导入您的库,如果您实际上在 Activity、Service 或其他 Context 子类中,请谨慎使用 Application Context
I have created a library, and added as a module in my app. My library does not have any activity class
, all classes are core java classes, so i don't have access to context
.
This is what i did;
- My library class is
singleton
- I have created a static variable like this;
static Context myGlobalContext
- I created a method where i set this context;
public void init(Context context){ myGlobalContext=context; }
I set this context from the first activity;
MyClass.sharedInstance().init(MyApplication.getAppContext());
then i use this myGlobalContext
in all of the library classes.
This thing works fine when i debug this, the value of myGlobalContext
is never null, but i am getting some crashes from crashlytics, where context
is null.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
When i run lint
, it also warns about the static global context
This is my application class
, here i create context;
public class MyApplication extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
Replace
MyApplication.context = getApplicationContext();
With
MyApplication.context = this;
And you need to add the application name to the manifest for this Context to ever be set
However, if you have a core Java library, you should not be using an Application because you have no life cycle methods to handle. Just define methods that use Context objects, import your library externally, and use Application Context sparingly if you're actually in an Activity, Service, or other Context subclass
这篇关于如何在不使用静态上下文的情况下将上下文传递给模块类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!