2011-12-01 101 views
0

当我改变手机的GPS位置该程序刚刚崩溃,它似乎并没有找到谷歌地图编号(查找ID) 这是我的代码:Android GPS,地图不会改变位置

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中(这不可能是错的,但如果你们问)

<?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

回答

2

好,这里是东西:Firstl Ÿ,尽量考虑如何构建你的程序。

您正在注册活动的onCreate方法中的位置侦听器。然后在onLocationChanged方法中,您要做的是创建一个同一活动的新实例,从而导致再次调用onCreate。所以你再次注册同一个监听器,并且当这个位置一次又一次地改变,等等。我不确定NullPointer来自哪里,但我相信这与您创建的无限循环有关。

这不是你应该怎么做的。最好的方法是让你的LocationListener作为一个服务工作(即独立于“托管”MapView的活动),所以在onLocationChanged方法中,你必须发送一个意图给活动,它将更新视图和位置地图。

希望这是有道理的。 PS:我相信你是Android开发的新手,所以在开始编码之前,请确保你完全理解了http://developer.android.com/guide/topics/fundamentals.html。另外:http://developer.android.com/guide/topics/fundamentals/activities.html和这个:http://developer.android.com/guide/topics/fundamentals/services.html