Default context for AlertDialog.Builder for map view?
-
27-09-2019 - |
문제
I've been trying to follow this tutorial on using Google Map View in Android. In the second part, they create an mContext
member variable and then pass that into AlertDialog.Builder(mContext)
, but the constructor that they call to create an instance of an HelloItemizedOverlay
doesn't instantiate mContext
, so it's just left null
as far as I know.
The program doesn't work properly, and throws a NullPointerException
as I suspected. How is this supposed to work?
Thanks,
Jengerer
해결책
In the tutorial, step 2 (the onCreate section) they instantiate a HelloItemizedOverlay:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
But notice that in the HelloItemizedOverlay constructor, it takes a Drawable and Context:
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
So the fault is with the tutorial. They're instantiating the class without a Context object, making it null. In your onCreate() method, just add this
to the constructor.
다른 팁
public HelloItemizedOverlay(Drawable defaultMarker, Context context) { super(boundCenterBottom(defaultMarker)); mContext = context; }
Create a single constructor for HelloItemizedOverlay
accepting two parameters: drawable
and this
.
The tutorial sounds like you need to make two constructors for different number of parameters.