2

Right now I am trying to design an Android app where a critical feature is the ability to click on an area and set a flag there.
To teach myself how the Google Maps API work, I used the tutorial in the dev guide: http://developer.android.com/resources/tutorials/views/hello-mapview.html

At each step of the way, I tried to understand exactly what was going on. And for the most part, I got it. The one thing I don't understand are the references to Context. Specifically, in the HelloItemizedOverlay class in that tutorial, there is the line

Context mContext;

and later on there are two constructors, one of which takes a Context object as an argument and the other of which does not.

public HelloItemizedOverlay(Drawable defaultMarker){
        super(boundCenterBottom(defaultMarker));
}

public HelloItemizedOverlay(Drawable defaultMarker, Context context){
        super(boundCenterBottom(defaultMarker));
        mContext = context;
}

The Context item appears to come into play later on in this class, in the onTap method:

@Override
protected boolean onTap(int index){
    OverlayItem item = mOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}

However, the tutorial doesn't explain how AlertDialog.Builder works - it just kind of throws it out there. And it seems like understanding the purpose of this is critical to understanding why the Context object exists.

In the main (and only) activity for this tutorial, the following lines seem to be the ones that interact with the HelloItemizedOverlay class:

List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
**HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);**

GeoPoint point = new GeoPoint(19240000, -99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hello World!", "Ciudad Mexico");

itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);

The line marked with ** is the line that references the constructor for the HelloItemizedOverlay class. However, that line turned out to produce a NullPointerException error in the onTap method, which makes sense because that method utilizes mContext, which is never given a value. After searching through StackOverflow, I found that this was an error in the tutorial, and that it could be fixed by changing the line to

**HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);**

Which, indeed, worked.

So my questions are:

What is Context? I wasn't really able to find a clear and applicable answer on the internet.

Why and how is Context used in this tutorial and in the Google Maps set up in general?

What is up with the two constructors?

Thanks!

Andrew Latham
  • 5,982
  • 14
  • 47
  • 87
  • http://stackoverflow.com/questions/3572463/what-is-context-in-android – aromero Dec 30 '11 at 21:30
  • I read that before, but I didn't understand how or why it's being used in this Context. It seems like it must be more than the answer in that question described, because it seems to me that the current state of the app doesn't really come into play. – Andrew Latham Dec 30 '11 at 21:32
  • Have you had a look at http://developer.android.com/reference/android/content/Context.html ? – user634618 Dec 30 '11 at 21:34
  • Yes, it wasn't very helpful. I don't see where any methods or properties of Context are being explicitly referenced, I only see it being passed as an argument. – Andrew Latham Dec 30 '11 at 21:36

2 Answers2

1

What is Context?

Context is an ancestor class of Activity (and other classes). It provides access to things like resources. You often need to supply a Context to various methods and constructors. If you are in a component (e.g., a MapActivity), using this should suffice in most cases.

Why and how is Context used in this tutorial and in the Google Maps set up in general?

Your MapActivity inherits from Context.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Could you expand on how MapActivity inherits from Context? I only see Context being passed as an argument to AlertDialog.Builder and not used otherwise, so is Context taking information from MapActivity and sharing it with AlertDialog.Builder? And why does the tutorial add two constructors when a Context is always passed? – Andrew Latham Dec 30 '11 at 22:33
  • http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapActivity.html – user802421 Dec 30 '11 at 22:35
  • OK, I understand now how MapActivity inherits from Context. What of the other two questions, though? – Andrew Latham Dec 30 '11 at 22:52
  • @AndrewLatham: "And why does the tutorial add two constructors when a Context is always passed?" -- you would have to ask the author of the tutorial that question. I did not write the tutorial. – CommonsWare Dec 30 '11 at 23:29
0

You may need the application Context for several reasons. In this tutorial you need it to create a pop-up dialog when user taps on an instance of ItemizedOverlay. This dialog shows the title and snippet texts of that item.

dmaxi
  • 3,267
  • 1
  • 18
  • 15
  • How is it used to create a pop-up? It appears to me that the information passed to the method generating the pop-up is all related to the particular item selected, not the state of the activity. Or is the argument required in that method for reasons not used in this tutorial? – Andrew Latham Dec 30 '11 at 22:57
  • The application context may be used by AlertDialog.Builder if you e.g. want to set the dialog title to a string resource (a string in your res/strings). In this case you can think of context as a way to get to all your application resources like drawables, strings, layouts, etc. – aspartame Dec 30 '11 at 23:12