如何在android中启动活动时显示值

发布于 2024-10-24 07:39:01 字数 1866 浏览 1 评论 0原文

嗨,我是一名新开发人员。在我的应用程序中,我试图显示一个地方的纬度和经度。当应用程序启动时,我希望自动显示值以下是我的代码。

{
  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没企图 2024-10-31 07:39:01

您可能在这里得到空列表:
String param = (String)myManager.getProviders(true).get(0); 并尝试获取列表中的第一个会导致 java.lang.IndexOutOfBoundsException 尝试添加
< /code> 到你的菜单。

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 causes java.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.

dawn曙光 2024-10-31 07:39:01

看起来您没有获得有效的位置。

你打开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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文