0

I'm working through Google's tutorial on how to create custom dialogs Located here: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

I entered everything in word or word and triple checked to make sure it is all correct but no matter how many times I check the code my program crashes. I have managed to fix it by changing a single line of code.

I changed the code from this

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

To this

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(MainActivity.this);

My question is, why doesn't the getApplicationContext() function return the right value. Since the code is from the official Google website, I'm assuming that I am doing something wrong.

I have included all my source code. I apologize if this is to spammy.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<Button 
    android:text="Launch Custom Dialog" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button>
</LinearLayout>

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity 
{
    Button btn1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Context mContext = getApplicationContext();
                Dialog dialog = new Dialog(MainActivity.this);

                dialog.setContentView(R.layout.custom_dialog);
                dialog.setTitle("Custom Dialog");

                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Hello, this is a custom dialog!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.icon);
                dialog.show();
            }
        });
    }
}
user786362
  • 113
  • 1
  • 5

3 Answers3

2

If you look in documentation http://developer.android.com/reference/android/content/Context.html#getApplicationContext%28%29

It says that getApplicationContext() returns the context of the single, global Application object of the current process

However, to create dialog you need context which is your parent activity. That's why it fails.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
1

I wrote an answer to similar problem on this question.I am posting here again for your convenience

As it turns out, Context of an activity is different then object returned by getApplicationContext(). This you can check by using logging, just output ActivityName.this and getApplicationContext.

The object returned by getApplicationContext is a global thing while context of an activity, well, belongs to that activity only.

Log.e(tag,""+ getApplicationContext());
Log.e(tag,""+CustomDialogActivity.this);

where CustomDialogActivity is my activity in which I want to show my dialog.

Dialogs require context of an activity and getApplicationContext() does not provide that. As written here (read comments) context of an activity is superset of getApplicationContext(). so It is a good thing to always pass context of an activity rather then the global context.

Community
  • 1
  • 1
1

I think it's the fact that it's inside the OnClickListener class will probably screw it up. getApplicationContext() is part of the Activity, once you're inside of your listener class it goes out of scope. I think you can do:

   MainActivity.this.getApplicationContext() 

which would work. Though I'm not sure about visibility, you might not be able to do that. However, doing MainActivity.this is just fine because that gives you a Context object as well.

dmon
  • 30,048
  • 8
  • 87
  • 96