Get application context returns null

后端 未结 6 868
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 18:49

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 n

相关标签:
6条回答
  • 2020-11-27 19:27

    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.

    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

    <application android:name="com.mypackage.mypackage.MyApp">
    ...
    ...
    ...
    </application>
    
    0 讨论(0)
  • 2020-11-27 19:27

    Create a static instance of the Context in your OnCreate and keep it till you want to get it from a getter method getContext()

    From the Application class:

    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:

    <application android:name="com.package.name.MyApp">
    
    0 讨论(0)
  • 2020-11-27 19:35

    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?

    0 讨论(0)
  • 2020-11-27 19:39

    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.

    0 讨论(0)
  • 2020-11-27 19:47

    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.

    0 讨论(0)
  • 2020-11-27 19:49

    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 Manifest file

    <application android:name="packagename.MyApp"/>
    

    to get the context use MyApp.getInstance().getApplicationContext()

    0 讨论(0)
提交回复
热议问题