I paste some piece of my code here:
public void onCreate(Bundle savedInstanceState) {
Log.e("Main Activity", "OnCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main_frame);
//GlowEffect is the custom class's constructor:GlowEffect(context);
new GlowEffect2(getApplictionContext());
AlertDialog.Builder builder = new Builder(this);
...
...
I find that when I change the param in **Builder(this)** to Builder(getApplicationContext());
The app will crash! While the GlowEffect2 constructor works well whatever the param is “this” or "getApplicationContext()".
So what the difference between the two params?
As you’ve noticed, the constructor for Builder takes a Context object as argument.
The Activity and Application objects are both subclasses of Context, so either would be a valid parameter to create a Builder.
What’s the difference between an Application and an Activity? I think one of the most obvious differences is their life cycles:
The Activity will live for as long as that particular piece of UI is running, and will be destroyed and recreated in various situations, e.g. on an orientation change, or when the Activity is not being viewed and the Android OS needs to free some memory by destroying Activities that are not being viewed.
The Application will live for as long as the application itself is running.
As for your specific problem, what is the stack trace for the crash? This question says
getApplicationContext()might be null because the application is still starting up whilst the Activity is being created (the answer says the issue was fixed in Android OS 1.6).