Issue
I followed Google's Map View tutorial from this link http://developer.android.com/resources/tutorials/views/hello-mapview.html
I've done the first about Creating Map activity, but have problems with Adding Overlays. Here is the source code:
package rs.iz.stevy.wifi;
import java.util.ArrayList;
import android.graphics.drawable.Drawable;
import android.app.AlertDialog;
import android.content.Context;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class Overlay extends ItemizedOverlay {
public Overlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
Context mContext;
mContext = context;
}
private ArrayList<OverlayItem>mOverlays= new ArrayList<OverlayItem>();
public Overlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i); }
@Override
public int size() {
return mOverlays.size();
}
@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;
}
}
Error appear at this line: AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); Eclipse shows mContext cannot be resolved to variable.
Here is the Activity class:
rs.iz.stevy.wifi;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class WiFiKupacicaActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapa= (MapView) findViewById(R.id.Mapa1);
mapa.setBuiltInZoomControls(true);
mapa.setSatellite(true);
GeoPoint initGeoPoint = mapa.getMapCenter();
List<Overlay> mapOverlays = mapa.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.kupacica);
Overlay itemizedoverlay = new Overlay (drawable);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Eclipse show error Cannot instantiate Overlay at this line: Overlay itemizedoverlay = new Overlay (drawable);
If you see here any error that can be easily fixed please answer cause this are my first attempts to write an android app.
Solution
The first error is because you defined mContext as a local variable in the constructor instead of as an instance variable. To remedy, move the Context mContext;
line out of the constructor (put it right before this line public Overlay(Drawable defaultMarker, Context context) {
). You want to declare instance variables outside of any method, but inside the class itself.
Your second error occurs because Overlay is an Abstract class. You cannot instantiate an abstract class with "new". What you want to do here is instantiate your Overlay subclass. In your import statements, you're importing com.google.android.maps.Overlay
so it thinks you're instantiating that rather than your subclass. To fix, replace
Overlay itemizedoverlay = new Overlay (drawable);
with
rs.iz.stevy.wifi.Overlay itemizedoverlay = new rs.iz.stevy.wifi.Overlay (drawable);
In practice, you should avoid giving a class the name of an abstract class already in its own hierarchy (to avoid import errors like this).
Answered By - Chris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.