When i change the gps location of the phone the program just crashes, it don’t seem to find the google maps id (find by id)
This is my code:
package Maps.GeoLocation.Google;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapsGeoLocationActivity extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView mapV;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
public void maps(double pLati, double pLongi)
{
mapV = (MapView) findViewById(R.id.mapView);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
GeoP = new GeoPoint ((int) (pLati *1E6), (int) (pLongi *1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(13);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
class mylocationlistener implements LocationListener {
double pLong;
double pLat;
@Override
public void onLocationChanged(Location location) {
MapsGeoLocationActivity ff = new MapsGeoLocationActivity();
pLong = location.getLongitude();
pLat = location.getLatitude();
//textLat.setText(Double.toString(pLat));
//textLong.setText(Double.toString(pLong));
ff.maps(pLat, pLong);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Main.xml (This can’t be wrong, but in case you guys ask)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="apikey"
/>
</LinearLayout>
LogCat:
http://img195.imageshack.us/img195/5655/16325616.png
Ok, here is the thing: Firstly, try to think how you want to structure your program.
You are registering a location listener in the onCreate method of an activity. Then in the onLocationChanged method what you do is to create a new instance of the same activity, thus resulting in onCreate being called again. So you are registering the same listener again, and when the location changes again and again and so on. I am not exactly sure where the NullPointer comes from but I believe it is something to do with this infinite cycles that you have created.
This is not how you should do it. The best way is to have your LocationListener work as a service (i.e. independent of the activity which “hosts” the MapView), so in the onLocationChanged method you will have to send an intent to the activity which will update the view and the location on the map.
Hope this makes sense.
PS: I believe you are new to Android development, so make sure you PERFECTLY understand this http://developer.android.com/guide/topics/fundamentals.html before starting to code. Also: http://developer.android.com/guide/topics/fundamentals/activities.html and this: http://developer.android.com/guide/topics/fundamentals/services.html