【问题标题】:How to show values when the activity is launched in android如何在android中启动活动时显示值
【发布时间】:2011-03-22 10:00:56
【问题描述】:

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

{
  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

【问题讨论】:

  • 为什么你的函数 loadCoords() 应该返回 Coords 但你总是返回 null?如果你返回 void 并写 loadCoords();而不是坐标 coords = loadCoords();会发生什么?

标签: java android android-activity launch


【解决方案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 " /> 到你的清单。

【讨论】:

  • 另加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
【解决方案2】:

您似乎没有获得有效的位置信息。

你打开你的 GPS 了吗?

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

【讨论】:

    猜你喜欢
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 2017-11-06
    相关资源
    最近更新 更多