I am trying to get current running context in android, I tried to use:
<application android:name="com.xyz.MyApplication">
</application>
public class MyApplication extends Application
{
private static Context context;
public void onCreate()
{
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext()
{
return MyApplication.context;
}
}
When I try to use MyApplication.getAppContext(), it gives me the error
AndroidRuntime(14421): android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
This is working for me:
You will just need to call
MyApplication.getContext()on any part of your app.I’m assuming that the application XML tag is on the manifest.xml
You don’t need to create any instance of Application class, it will be created when you launch the app, before anything.