嗨,我是一名新开发人员。在我的应用程序中,我试图展示一个地方的经纬度。当应用程序启动时,自动我想要显示的值以下是我的代码。如何在android中启动活动时显示值
{
setContentView(R.layout.main);
if(entered)
{
loadCoords();
}
Coords coords = loadCoords();
}
private Coords loadCoords()
{
TextView latText = (TextView) findViewById(R.id.latText);
TextView lngText = (TextView) findViewById(R.id.lngText);
LocationManager myManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(myManager != null){
//List list = myManager.getAllProviders();
String param = (String)myManager.getProviders(true).get(0);
Location loc = myManager.getLastKnownLocation(param);
if(loc != null){
Double latPoint = loc.getLatitude();
Double lngPoint = loc.getLongitude();
try {
latText.setText(latPoint.toString());
lngText.setText(lngPoint.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
else
Log.e("AndroidMaps ","Error: Location is null");
}
else
Log.e("AndroidMaps ","Error: Location Manager is null");
return null;
}
}
但我的应用程序崩溃时,我试图在设备中运行它。
以下是logcat的
ERROR/AndroidRuntime(6311): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(6311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gis.ss/com.gis.ss.main}: java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
ERROR/AndroidRuntime(6311): Caused by: java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
表示任何人都可以给我解释一下什么是错误的错误... plzzz
为什么你的函数loadCoords()应该返回Coords,但你总是返回null?如果你返回void并写入loadCoords();而不是Coords coords = loadCoords();怎么了? – jcfrei 2011-03-22 10:07:51