2011-09-06 54 views
0

我想从一个EditText中获取一个字符串的字符,并且同时想显示当前位置的经度和纬度。EditText问题

这里显示的代码

Button btn=(Button)findViewById(R.id.format); 


btn.setOnClickListener(new Button.OnClickListener() { 

    public void onClick(View v) { 

       showCurrentLocation(); 
     } 
     }); 

protected void showCurrentLocation() { 

     text=name.getText().toString(); 

     String finalText = ""; 
     finalText = Character.toString(text.charAt(3)); 

     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     if (location != null) { 
       String message = String.format(
           "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
           location.getLongitude(), location.getLatitude() 
       ); 
       Toast.makeText(GPSLocator.this, message, 
           Toast.LENGTH_LONG).show(); 
     } 

} 

的一部分编译代码时我给任何字符并单击发送按钮,则显示一条消息在屏幕上像“应用GPSLocator(处理COM之后。 vetexVervelnc GPSLocator)已意外停止,请重试ForSE召开前夕关闭“

回答

2

尝试这样

finalText = ""+text.charAt(3); 

确保将正确的权限在清单文件中

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>  
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
0

你能发布确切的错误吗?

以下代码可能对您有所帮助。

try{ 
    finalText = Character.toString(text.charAt(3)); 
    } catch {Exception e) { 
    finalText=""; 
}