android-context

Calling context.getResources() returns null

妖精的绣舞 提交于 2019-11-28 00:08:03
问题 So I am trying to get a string resource in my project but when I called context.getResources().getString(...) , I get a NullPointerException . In debug mode, I found out that the context isn't null but looking at its members, I found out that mResources was null. Why are the resources not loaded for the activity context? EDIT Apparently, this is what triggered the Exception public class MyActivity extends Activity { SomeClass someClass = new SomeClass(this); @Override protected void onCreate

How to start an Intent if context is not Activity Context but Application Context

家住魔仙堡 提交于 2019-11-27 23:19:15
问题 I'm trying to start an activity from a class that extends BroadcastReceiver. public void onReceive(Context context, Intent intent) { the problem is that parameter context is the Application context and not an Activity context. Is there a way start an intent using the Application context? 回答1: Here is sample code how to call another activity using context, set flag as per your requirement: public void onReceive(Context context, Intent intent) { Intent startActivity = new Intent();

Dagger 2 injecting Android Context

别来无恙 提交于 2019-11-27 22:42:28
I am using Dagger 2 and have it working however I now need access to the Android Application Context. Its not clear to me how to inject and get access to the context. I have tried to do this as follows: @Module public class MainActivityModule { private final Context context; MainActivityModule(Context context) { this.context = context; } @Provides @Singleton Context provideContext() { return context; } However this results in the following exception: java.lang.RuntimeException: Unable to create application : java.lang.IllegalStateException: mainActivityModule must be set If I inspect the

Android context leaks in AsyncTask

妖精的绣舞 提交于 2019-11-27 22:14:27
问题 If I interpret this article correctly, passing the activity context to AsyncTasks is a potential leak, as the activity might be destroyed while the task is still running. How do you deal with this in AsyncTasks that are not inner clases and need access to resources or to update the UI? Additionally, how can you avoid leaking the context if you need references to progress dialogs to dismiss them? 回答1: If I understand your question correctly: Java's WeakReference or SoftReference class is a

How do you obtain a Drawable object from a resource id in android package?

蓝咒 提交于 2019-11-27 19:53:19
问题 I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* package? for example if drawableId was android.R.drawable.ic_delete mContext.getResources().getDrawable(drawableId) 回答1: Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email); ImageView image = (ImageView)findViewById(R.id.image); image.setImageDrawable(d); 回答2: As of API 21 , you should use the

Difference between getApplicationContext and classname.this

烈酒焚心 提交于 2019-11-27 18:34:39
问题 When I'm using list view and I have a custom Base Adapter class, I get different text color in list view when base adapter is instantiated by getApplicationContext and classname.this . By getApplicationContext I get white text color but classname.this is black. Can anyone explain it for me? 回答1: Basically they are both instances of Context, but the difference is application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an

Passing a activity context into a static method, memory leak potential?

a 夏天 提交于 2019-11-27 18:00:42
问题 I've seen this particular technique for launching activities and it seems to me like a bad idea because of static contexts but I was hoping someone might have a legit reason behind this approach. The activity you want to launch implements a static launch(Context context) method that sets up the intent, flags, etc and finally starts the activity. public static void launch(Context context){ Intent i = new Intent(context, SomeOtherActivity.class); // flag stuff context.startActivity(i); } Then a

Finish the calling activity when AsyncTask completes

女生的网名这么多〃 提交于 2019-11-27 17:42:59
问题 My calling activity: public class Hello extends Activity { public void onCreate(Bundle savedInstanceState) { MyTask mt = new MyTask(this); mt.execute(); } Now In MyTask (an external class): public class MyTask extends AsyncTask<Void, Void, Void> { private Context mContext; public MyTask(Context context) { mContext = context; } //doinbackground, etc protected void onPostExecute() { mContext.finish(); } Other things are working as expected if I remove mContext.finish() above. But if I'm calling

Accessing Resources without a Context

别等时光非礼了梦想. 提交于 2019-11-27 14:18:39
I'm trying to put configuration, such as URLs/etc, into a resource folder for a utility class to use. However, I don't want to pass the Context from the activities everywhere. I would like to be able to access a resource via a path name (seems like assets/ was designed for this use), without using a context to access the resource. In this particular case, I want a singleton to use something in configuration when it's instantiated. It has no need for anything from resources besides that one time during instantiation. Therefore, having to pass in a Context every time getInstance() is called

Is it safe to save the app context to a static variable in Android?

偶尔善良 提交于 2019-11-27 13:51:36
I know that usage of static variables on Android is quite risky, especially if you reference them to activities. However, if I have a class that extends Application (let's call this class "App"), is it safe to reference to the instance of this class? If so, is it also safe for any other class to have any kind of reference to the application context? I mean, can there be a memory leak if I have a reference to the application context in any kind of class? The purpose is that no matter in which scope I am in, I can always get a reference to the application context. I think it's safe, since if the