android-context

Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

北慕城南 提交于 2019-11-26 09:09:54
问题 In my ongoing learning process (dialog boxes this time), I discovered that this works: AlertDialog.Builder builder = new AlertDialog.Builder(this); While the following doesn\'t work (fails at runtime with WindowManager$BadTokenException): AlertDialog.Builder builder = new AlertDialog.Builder(this.getApplicationContext()); I don\'t understand why, because the constructor for AlertDialog.Builder is defined to accept Context as a parameter, not Activity: public AlertDialog.Builder (Context

Why getApplicationContext() in constructor of Activity throws null pointer exception?

南楼画角 提交于 2019-11-26 08:37:07
问题 After some time spent bug hunting it turns out that : public class MainActivity extends BaseActivity { // BaseActivity extends Activity public MainActivity() { super(); getApplicationContext(); // NPE here } } Why ? Where is this documented ? Froyo 回答1: Wait for the end of onCreate to call this method. public class MainActivity extends BaseActivity { public onCreate(Bundle savedInstanceState) { super(savedInstanceState); getApplicationContext(); //activity has a context now } } 回答2: Just to

Get application context from non activity singleton class

跟風遠走 提交于 2019-11-26 06:38:56
问题 In my android project, I have ImageAdapter class in which I pass app context for some further needs. public class ImageAdapter extends BaseAdapter { private Context c; public ImageAdapter(Context c) { this.c = c; } ... } The problem is that I wanna make ImageAdapter as a singleton to have an easy access to the instance of this class from all of my activities. But I have no idea how to pass app context from getApplicationContext() method from one of my activities to ImageAdapter. So is there

How can I share a SharedPreferences file across two different android apps?

好久不见. 提交于 2019-11-26 06:38:36
问题 I\'ve been struggling with this for a while. Basically, I want to have two applications (which will always be installed together) share preferences, with one of them being just a service which runs in the background and needs to use the preferences (should own the preferences but only really needs to read them) and the other app being a front-end UI app which needs to be able to write to the preferences file owned by the other app. The service will be doing things in the background (which may

Android : Static Fields and Memory Leaks

早过忘川 提交于 2019-11-26 06:35:49
问题 I\'ve been studying up on best practices for preventing Context/Activity memory leaks when creating views, and I can\'t seem to find a definite answer on what is or is not allowed when it comes to static fields in classes. Let\'s say I have a code of this form: public class MyOuterClass extends Activity{ private MyInnerClass; MyInnerClass = (MyInnerClass) findViewById(<XML call here>); MyInnerClass.myXInt = 3; // onCreate(), onResume(), etc. public static class MyInnerClass extends

getApplicationContext(), getBaseContext(), getApplication(), getParent()

我的未来我决定 提交于 2019-11-26 06:05:05
问题 What is the difference between: getApplicationContext() getBasecontext() getApplication() getParent() Can you elaborate with one simple example? 回答1: getApplicationContext() Application context is associated with the Application and will always be the same throughout the life cycle. getBasecontext() should not be used, just use Context instead of it which is associated with the activity and can be destroyed when the activity is destroyed. 回答2: getApplicationContext() Application context is

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

自作多情 提交于 2019-11-26 05:56:36
I'm new to android and I'm trying to understand the difference between getApplication() , getApplicationContext( ), getBaseContext() , getContext() and someClass.this and especially when to use the these methods in the following code lines: When I launch a toast what is the difference between these and in which cases to I use them? Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show(); Toast.makeText

get Context in non-Activity class [duplicate]

冷暖自知 提交于 2019-11-26 05:26:07
问题 This question already has answers here : How to retrieve a context from a non-activity class? (4 answers) Closed 6 years ago . In an android Application, is there any way to get the context in android in a non-activity class if the activity class name is known? 回答1: If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows: class YourNonActivityClass{ // variable to hold context private

getting context in AsyncTask

眉间皱痕 提交于 2019-11-26 05:24:05
问题 I am trying to get the context in my AsyncTask of the class called Opciones(this class is the only one that call that task) but I don\'t know how to do it, I saw some code like this: protected void onPostExecute(Long result) { Toast.makeText(Opciones.this,\"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: \", Toast.LENGTH_LONG).show(); } But it doesn\'t work for me it says: \"No enclosing instance of the type Opciones in scope\" 回答1: You need to do following things. when you want

getActivity() returns null in Fragment function

混江龙づ霸主 提交于 2019-11-26 03:14:52
问题 I have a fragment (F1) with a public method like this public void asd() { if (getActivity() == null) { Log.d(\"yes\",\"it is null\"); } } and yes when I call it (from the Activity), it it is null... FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); F1 f1 = new F1(); transaction1.replace(R.id.upperPart, f1); transaction1.commit(); f1.asd(); It must be something that I am doing very wrong, but I don\'t know what that is 回答1: commit schedules the transaction, i.e