Android 获取应用程序上下文返回空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21994612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get application context returns null
提问by learner
The following schema has been touted as the way to get application context from anywhere within my android app. But sometimes doing MyApp.getContext()
returns null. I tried changing the schema by removing static
from getContext()
so that I would do MyApp.getInstance().getContext()
. It still returns null. How do I fix this? How do I get my application's context from anywhere within my app?
以下架构被吹捧为从我的 android 应用程序中的任何位置获取应用程序上下文的方式。但有时做MyApp.getContext()
返回空。我尝试通过删除static
from 来更改架构,getContext()
以便我可以这样做MyApp.getInstance().getContext()
。它仍然返回空值。我该如何解决?如何从应用程序内的任何位置获取应用程序的上下文?
public class MyApp extends Application {
private static MyApp instance;
public static MyApp getInstance() {
return instance;
}
public static Context getContext() {
return instance.getApplicationContext();
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
}
回答by Jorgesys
Create in onCreate()
an instance of getApplicationContext()
(mContext
) then call MyApp.getContext()
from everywhere in your app and you will get your application context statically.
在( )onCreate()
的实例中创建,然后 从应用程序的任何地方调用,您将静态获取应用程序上下文。getApplicationContext()
mContext
MyApp.getContext()
public class MyApp extends Application {
//private static MyApp instance;
private static Context mContext;
public static MyApp getInstance() {
return instance;
}
public static Context getContext() {
// return instance.getApplicationContext();
return mContext;
}
@Override
public void onCreate() {
super.onCreate();
// instance = this;
mContext = getApplicationContext();
}
}
Remember to declare into your AndroidManifest.xml
记得声明到你的 AndroidManifest.xml
<application android:name="com.mypackage.mypackage.MyApp">
...
...
...
</application>
回答by Sami Eltamawy
Create a static instance of the Context
in your OnCreate
and keep it till you want to get it from
a getter method getContext()
Context
在您的中创建一个静态实例OnCreate
并保留它,直到您想从 getter 方法中获取它getContext()
From the Application
class:
从Application
班级:
public class MyApp extends Application {
private static Context sContext;
@Override
public void onCreate() {
sContext = getApplicationContext();
super.onCreate();
}
public static Context getContext() {
return sContext;
}
}
Declare it in your Manifest
:
在您的Manifest
:
<application android:name="com.package.name.MyApp">
回答by Ankit Yadav
Use the following way to get the Application context.
使用以下方式获取应用程序上下文。
public class MyApp extends Application {
private static MyApp mAppInstance=null;
public static Context appContext;
public static MyApp getInstance() {
return mAppInstance;
}
public static MyApp get() {
return get(appContext);
}
public static MyApp get(Context context) {
return (MyApp) context.getApplicationContext();
}
@Override
public void onCreate() {
super.onCreate();
mAppInstance=this;
appContext=getApplicationContext();
}
}
add the the application name inside the Manifestfile
在清单文件中添加应用程序名称
<application android:name="packagename.MyApp"/>
to get the context use
MyApp.getInstance().getApplicationContext()
获取上下文使用
MyApp.getInstance().getApplicationContext()
回答by Code-Apprentice
instance
is never initialized and so has a default value of null
. This means that instance.getContext()
will throw a NullPointerException
. To fix this, you need to initialize the instance
variable.
instance
从未初始化,因此具有默认值null
。这意味着instance.getContext()
将抛出一个NullPointerException
. 要解决此问题,您需要初始化instance
变量。
回答by ucsunil
Currently, you have not initialized instance and by default it's value would now be set to null. You need to assign it a value before you can use it.
当前,您尚未初始化实例,默认情况下,它的值现在将设置为 null。您需要先为其分配一个值,然后才能使用它。
回答by Cheok Yan Cheng
Another root cause, is due to the buggy backup process. Please refer to
另一个根本原因是由于错误的备份过程。请参阅
Why backup related process might cause Application's onCreate is not executed?