开发者

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 = (开发者_JS百科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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜