Issue
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!
Solution
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
.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.