简体   繁体   English

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

[英]How to show values when the activity is launched in android

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 以下是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 谁能解释我的错误是什么... plzzz

You are probably getting and empty list here: String param = (String)myManager.getProviders(true).get(0); 您可能会在这里空列表: 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. 并尝试获取列表的第一个导致java.lang.IndexOutOfBoundsException尝试添加<uses-permission android:name="android.permission.ACCESS_GPS" /><uses-permission android:name="android.permission.ACCESS_LOCATION " />以表达自己的意思。

Looks like you are not getting valid location. 看来您没有获得有效的位置。

Have you turned on your GPS?? 您是否打开了GPS?

If you are running it on Emulator only then you have to set your location in DDMS - > Emulator Control -> Location Controls. 如果仅在Emulator上运行它,则必须在DDMS-> Emulator Control-> Location Controls中设置位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Activity在启动时崩溃 - Android Activity crashing when launched 如何检测何时启动活动(Android Jelly Bean) - How to detect when an activity is launched ( Android Jelly Bean ) Android-单击启动图标或单击后退按钮时如何恢复上一个活动 - Android - How to resume the last activity when launched icon is clicked or when back button is clicked 当我关闭启动它的活动时,Android Service死亡 - Android Service dies in when I close the Activity that launched it 在Android 4.0版上单击通知时,永远不会启动Activity - Activity is never launched when the notification is clicked on android version 4.0 启动应用程序时发生的活动 - Activity that happens when the app is launched 活动首次启动时如何停止广播接收器被触发 - How to stop broadcast receiver to be fired when activity first launched MediaPlayer - 如何在第一次启动应用程序时显示警报对话框 - MediaPlayer - How to show a alert dialoge when app is launched first time Android:如何在返回Activity时在EditText中保存值 - Android:How to save values in EditText when returning to an Activity Android-启动活动时不显示“加载”对话框,而仅在某些活动中 - Android - Loading Dialog not appearing when an activity is launched, but only from certain activities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM