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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:24:32  来源:igfitidea点击:

Get application context returns null

androidapplicationcontext

提问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 staticfrom 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()返回空。我尝试通过删除staticfrom 来更改架构,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()mContextMyApp.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 Contextin your OnCreateand keep it till you want to get it from a getter method getContext()

Context在您的中创建一个静态实例OnCreate并保留它,直到您想从 getter 方法中获取它getContext()

From the Applicationclass:

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

instanceis 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 instancevariable.

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?

为什么备份相关进程可能会导致Application的onCreate没有执行?