如何在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
hi i am a new developer. In my app i am trying to show the latitude and longitude of a place. When the app is started, automatically i want the values to be shown Following is my code.
{
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;
}
}
but my app gets crashed when i am trying to run it in a device.
the following is the error shown in 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
can anyone explain me what is the error...plzzz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能在这里得到空列表:
< /code> 到你的菜单。
String param = (String)myManager.getProviders(true).get(0);
并尝试获取列表中的第一个会导致java.lang.IndexOutOfBoundsException
尝试添加
和You are probably getting and empty list here:
String param = (String)myManager.getProviders(true).get(0);
and trying to get the first of the list causesjava.lang.IndexOutOfBoundsException
try to add<uses-permission android:name="android.permission.ACCESS_GPS" />
and<uses-permission android:name="android.permission.ACCESS_LOCATION " />
to your menifest.看起来您没有获得有效的位置。
你打开GPS了吗?
如果您仅在模拟器上运行它,那么您必须在 DDMS 中设置您的位置 - >仿真器控制->位置控制。
Looks like you are not getting valid location.
Have you turned on your GPS??
If you are running it on Emulator only then you have to set your location in DDMS - > Emulator Control -> Location Controls.