문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top