我正在尝试创建一个只显示屏幕坐标的应用程序。清单文件中添加了以下权限。在android中获取位置
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
以下是MainActivity.java
import com.example.location.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.provider.Settings;
public class MainActivity extends Activity {
double latitude;
double longitude;
TextView lat, longi, loc, status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat = (TextView)findViewById(R.id.latitude);
longi = (TextView)findViewById(R.id.longitude);
status = (TextView)findViewById(R.id.status);
loc = (TextView)findViewById(R.id.location);
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
if(gpsStatus){
status.setText("GPS_PROVIDER is enabled.");
// This is to check if GPS_PROVIDER is enabled or not.
// This is working.
}
}
@Override
protected void onResume(){
super.onResume();
loc.setText("Entered Resume method"); //This works as well.
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
String str_lat = String.valueOf(latitude);
String str_longi = String.valueOf(longitude);
lat.setText(str_lat);
longi.setText(str_longi);
loc.setText((CharSequence) location);
}
@Override
public void onStatusChanged(String provider, int status,Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//Location Location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//double altitude = Location.getAltitude();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
}
}
代码的应用程序不列入的setText的纬度和经度的文本,当在模拟器中运行,我DONOT看到任何错误。我甚至没有得到0.0,0.0的经度和纬度。
虽然运行在手机应用程序,几秒钟后,应用程序与对话框关闭“不幸的是,应用程序已停止”
以下是在logcat中的错误消息。
11-04 23:35:48.250: E/AndroidRuntime(1224): FATAL EXCEPTION: main
11-04 23:35:48.250: E/AndroidRuntime(1224): Process: com.example.location, PID: 1224
11-04 23:35:48.250: E/AndroidRuntime(1224): java.lang.ClassCastException: android.location.Location cannot be cast to java.lang.CharSequence
11-04 23:35:48.250: E/AndroidRuntime(1224): at com.example.location.MainActivity$1.onLocationChanged(MainActivity.java:65)
11-04 23:35:48.250: E/AndroidRuntime(1224): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
11-04 23:35:48.250: E/AndroidRuntime(1224): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
11-04 23:35:48.250: E/AndroidRuntime(1224): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
11-04 23:35:48.250: E/AndroidRuntime(1224): at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 23:35:48.250: E/AndroidRuntime(1224): at android.os.Looper.loop(Looper.java:136)
11-04 23:35:48.250: E/AndroidRuntime(1224): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-04 23:35:48.250: E/AndroidRuntime(1224): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 23:35:48.250: E/AndroidRuntime(1224): at java.lang.reflect.Method.invoke(Method.java:515)
11-04 23:35:48.250: E/AndroidRuntime(1224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-04 23:35:48.250: E/AndroidRuntime(1224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-04 23:35:48.250: E/AndroidRuntime(1224): at dalvik.system.NativeStart.main(Native Method)
我是新来的android编程,所以kindle纠正我,如果我正在做任何愚蠢的错误,并帮助我学习。任何帮助调试这是非常感谢。
这意味着您的位置对象为空 – 2014-11-05 04:31:02
请将您在Logcat中看到的错误发布到“不幸,应用程序已停止”时出现。 http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator是一个很好的指针,通过模拟器获取位置值。 – 2014-11-05 04:33:02
@VnyKumar:这真的有帮助,我试图显示作为一个分析序列的整个位置的内容。那是错误。 – user3543477 2014-11-05 04:44:14