Android 谷歌地图中的当前位置,并将收到的位置存储在TextView上

Android 谷歌地图中的当前位置,并将收到的位置存储在TextView上,android,google-maps,gps,textview,android-location,Android,Google Maps,Gps,Textview,Android Location,我是android新手,通过许多教程,通过自动缩放获得用户当前位置,并将该位置的纬度和经度存储到文本视图中。到目前为止,我无法获得位置,无法获得连续的错误和崩溃。同时,我无法显示gpsStatus和networkConnectionStatus的状态。我和targetSDKlevel 23一起工作 答案是肯定的 这是密码 } 日志: 04-01 15:40:29.334 22356-22675/? W/ActivityThread: ClassLoader.loadClass: The

我是android新手,通过许多教程,通过自动缩放获得用户当前位置,并将该位置的纬度和经度存储到文本视图中。到目前为止,我无法获得位置,无法获得连续的错误和崩溃。同时,我无法显示gpsStatus和networkConnectionStatus的状态。我和targetSDKlevel 23一起工作

答案是肯定的

这是密码 }

日志:

04-01 15:40:29.334 22356-22675/? W/ActivityThread: ClassLoader.loadClass:     The class loader returned by Thread.getContextClassLoader() may fail for     processes that host multiple applications. You should explicitly specify a     context class loader. For example:     Thread.setContextClassLoader(getClass().getClassLoader());
04-01 15:40:29.490 22356-22356/? I/Choreographer: Skipped 154 frames!  The     application may be doing too much work on its main thread.
04-01 15:40:29.709 22655-22659/? D/dalvikvm: GC_CONCURRENT freed 732K, 22%     free 4617K/5912K, paused 5ms+10ms, total 43ms
04-01 15:40:29.709 22655-22655/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 38ms
04-01 15:40:29.724 22356-22356/? D/AndroidRuntime: Shutting down VM
04-01 15:40:29.724 22356-22356/? W/dalvikvm: threadid=1: thread exiting with     uncaught exception (group=0x41701ba8)
04-01 15:40:29.771 22356-22356/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.rescue,     PID: 22356
                                                       java.lang.NullPointerException
                                                   at     com.rescue.Maps.location(Maps.java:159)
                                                   at     com.rescue.Maps.setMap(Maps.java:125)
                                                   at     com.rescue.Maps.onMapReady(Maps.java:95)
                                                   at     com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                                                   at     com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
                                                   at     android.os.Binder.transact(Binder.java:361)
                                                   at     com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82)
                                                   at     maps.ei.bu$6.run(Unknown Source)
                                                   at     android.os.Handler.handleCallback(Handler.java:733)
                                                   at     android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at     android.os.Looper.loop(Looper.java:136)
                                                   at     android.app.ActivityThread.main(ActivityThread.java:5059)
                                                   at     java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                   at     dalvik.system.NativeStart.main(Native Method)
04-01 15:40:29.795 435-707/? W/ActivityManager:   Force finishing activity     com.rescue/.Maps

使用位置管理器

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

//Now set the value of Double to String
String Lat = String.valueOf(latitude);
String Lon = String.valueOf(longitude);

//Set value to textview

YourTextView1.setText(Lat);
YourTextView2.setText(Lon);
getLastKnownLocation()可能返回null,这可能导致应用程序因NullPointerException而崩溃

或者,您可以浏览Android提供的位置策略文档,并使用其中提供的方法:

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager)          this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
public void onLocationChanged(Location location) { 
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    // Display a marker on the GoogleMap
} 
public void onStatusChanged(String provider, int status, Bundle extras) {} 
public void onProviderEnabled(String provider) {} 
public void onProviderDisabled(String provider) {} 
}; 
// Register the listener with the Location Manager to receive location updates
// NOTE : PLEASE ADJUST THE FREQUENCY OF UPDATES PROPERLY
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

检查
logcat
输出以了解有关崩溃的更多详细信息!我只是浏览一下日志,但没有显示这样的错误。一旦我能够得到位置,但是为了测试,我从GPS和网络上退出,从那时起,我没有得到位置,只是得到异常错误。是否有可能不使用.getLastKnownLocation,而不是SDK级别23中的任何其他设备。您正在测试的设备我正在galaxy nexus(4.4.4)上测试。我尝试了此操作,但得到了NullPointException,请查看我之前的评论以了解详细信息。您是否在清单文件中授予了相应的权限?是的,发布Logcat的输出,以便我们能够找出错误的确切原因。确保您的设备gps已打开!!这可能是返回空值的原因添加在setMap()方法上,将您的locationmanager代码替换为从location manager()方法删除您的代码到public void location(..)我尝试了这个方法,但它不能正常工作,它确实启动了地图,但没有任何功能(没有自动缩放,没有标记)。这是因为您已定义了“全部在位置”方法。您是否从setMap()调用了该方法?是的,只有Maps启动,没有其他函数。
// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager)          this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
public void onLocationChanged(Location location) { 
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    // Display a marker on the GoogleMap
} 
public void onStatusChanged(String provider, int status, Bundle extras) {} 
public void onProviderEnabled(String provider) {} 
public void onProviderDisabled(String provider) {} 
}; 
// Register the listener with the Location Manager to receive location updates
// NOTE : PLEASE ADJUST THE FREQUENCY OF UPDATES PROPERLY
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);