2011-03-22 87 views
0

嗨,我是一名新开发人员。在我的应用程序中,我试图展示一个地方的经纬度。当应用程序启动时,自动我想要显示的值以下是我的代码。如何在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

+2

为什么你的函数loadCoords()应该返回Coords,但你总是返回null?如果你返回void并写入loadCoords();而不是Coords coords = loadCoords();怎么了? – jcfrei 2011-03-22 10:07:51

回答

1

你可能会得到这里空单: String param = (String)myManager.getProviders(true).get(0);,并试图得到第一个列表的原因java.lang.IndexOutOfBoundsException尝试 <uses-permission android:name="android.permission.ACCESS_GPS" /><uses-permission android:name="android.permission.ACCESS_LOCATION " />添加到您的menifest。

+0

也添加' Harshad 2011-03-22 10:11:16

0

看起来你没有得到有效的位置。

你打开你的GPS吗?

如果您在Emulator上运行它,那么您必须在DDMS - > Emulator Control - > Location Controls中设置您的位置。