Hick
Hick

Reputation: 36414

MapView coming null irrespective of what I do in Android. What might be the bug?

This is my LocationActivity:

public class LocationActivity extends MapActivity{

    GeoPoint p;
    Bundle bundle;

    ArrayList <String> address  = new ArrayList<String>();

    MapController mc;
    private LocationManager locationManager;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map);

        MapView mapView = (MapView)findViewById(R.id.mapView1);

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

         Criteria criteria = new Criteria();
           // criteria.setAccuracy(Criteria.ACCURACY_FINE);
             criteria.setAltitudeRequired(false);
             criteria.setBearingRequired(false);
             criteria.setCostAllowed(true);
             String strLocationProvider = lm.getBestProvider(criteria, true);

        Location location = lm.getLastKnownLocation(strLocationProvider);

        if (location == null)
        {
            Toast.makeText(LocationActivity.this, "working", Toast.LENGTH_LONG);
        }
        double   lat =  (double) location.getLongitude();
        double lon = (double) location.getLatitude();

        p = new GeoPoint(
              (int) (lat * 1E6), 
              (int) (lon * 1E6));

        mc = mapView1.getController();
        //        mc.setCenter(new GeoPoint(29450116, 77312191));
        mc.animateTo(p);
        mapView1.setClickable(true);
        mapView1.setBuiltInZoomControls(true);
        mapView1.setSatellite(true);
        mapView1.setTraffic(true);

        mapView1.invalidate();

    }

    @Override
    protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
    }
}

This is my map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <com.google.android.maps.MapView 
        android:id="@+id/mapView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:enabled="true"
        android:clickable="true"
         android:apiKey="04KR0_lkSRZxCk_JC9yIkk50f3ZE_JjBT7HJ6jw"
        />

    <Button 
        android:text="Add Location" 
        android:id="@+id/mapButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </Button>
</RelativeLayout>

I've cleaned my project. Created a new layout to check if it works or not but still, my MapView comes out null and I get a null pointer exception. What might be the error?

Upvotes: 0

Views: 400

Answers (3)

AnthonyW
AnthonyW

Reputation: 2008

I agree with px3j; if you have swithched MapView mapView = (MapView)findViewById(R.id.mapView1); to MapView mapView1 = (MapView)findViewById(R.id.mapView1); please update your question.

Upvotes: 1

biegleux
biegleux

Reputation: 13247

check getLastKnownLocation()

Throws IllegalArgumentException, if provider is null or doesn't exist

you have to use try-catch block:

mc = mapView.getController();
try {
    Location location = lm.getLastKnownLocation(strLocationProvider);
    if (location != null) {
        double lat =  (double) location.getLongitude();
        double lon = (double) location.getLatitude();
        p = new GeoPoint((int) (lat * 1E6),  (int) (lon * 1E6));
        mc.animateTo(p);
    }
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}

No provider was found based on your criteria, you have to adjust them.

Upvotes: 0

px3j
px3j

Reputation: 236

It looks like you are assigning to mapView but using mapView1.

    // Assigning the value to mapView
    MapView mapView = (MapView)findViewById(R.id.mapView1);

    // accessing mapView1
    mc = mapView1.getController();  // Is this generating the NPE?

Hope this helps...

Upvotes: 0

Related Questions