2011-05-26 121 views
1

这里是我的代码..我想从经度和纬度发现只是一个城市的名字.. 现在我得到这样的:无法从字符串[]转换为String

"Address[addressLines=[0:"Mustamäe tee 145",1:"12918 Tallinn",2:"Estonia"],feature=145,admin=Harju County,sub-admin=null,locality=Tallinn,thoroughfare=Mustamäe tee,postalCode=12918,countryCode=Estonia,countryName=null,hasLatitude=true,latitude=59.4123264,hasLongitude=true,longitude=24.6903851,phone=null,url=null,extras=null]" 

我试图将其转换字符串和拆分它..我做了一个字符串数组,但是当我尝试分配“城市名”字符串数组“名称”我得到这个错误 - “类型不匹配:不能从字符串[]转换为字符串”

Can请告诉我,我错在哪里?

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 
import android.content.Context; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
public class GPSHelper { 
static double latitude; 
static double longitude; 
static String[] name = new String[5]; 

public static void getCity(LocationManager lm, Context appContext) { 

    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    longitude = location.getLongitude(); 
    latitude = location.getLatitude(); 
    final LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 

     public void onProviderDisabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 

    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    Geocoder userLocation = new Geocoder(appContext, Locale.getDefault()); 
    List<Address> cityName; 
    try { 
     cityName = userLocation.getFromLocation(latitude, longitude, 1); 
     if(cityName != null && !cityName.isEmpty()) { 
      for(int i=0; i<5; i++){ 

      name[i] = cityName.get(0).toString().split(""); 

      } 
     } 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 

} 

回答

3

这里的所有答案都很好。 但我想知道为什么你会使用空字符分割?

你知道它返回单个字符数组(并在开始时没有)?

结帐代码:

String str ="dasd"; 
String[] strr = str.split(""); 
for(int i=0; i < strr.length;i++) 
    System.out.println("i="+i+";str="+strr[i]); 

输出:

i=0;str= 
i=1;str=d 
i=2;str=a 
i=3;str=s 
i=4;str=d 

我建议,既然cityName.get(0)返回地址对象必须有一个方法,该方法将返回它的名字,像下面。

name[i] = cityName.get(0).getName(); 

希望这会有所帮助。

+0

我这么笨:) ..谢谢....但是帮助这个:name = cityName.get(0).getLocality(); – Sergio 2011-05-26 10:31:42

+0

@Sergio乐于提供帮助。享受Java – Boro 2011-05-26 10:33:11

2
name[i] = cityName.get(0).toString().split(""); 

name[i]String类型而cityName.get(0).toString().split("")的类型是String[]

1
name[i] = cityName.get(0).toString().split(""); 

这里名称[Ⅰ]将持有只有字符串,但分割( “”)方法需要字符串数组中左侧。

这是真的需要循环?啥子有关删除for循环和代码它像

if(cityName != null && !cityName.isEmpty()) {   

     name[] = cityName.get(0).toString().split(""); 

    } 
0
cityName.get(0).toString().split(""); 

返回一个String[]的结果,但name[i]应该是一个String而不是String[]

你为什么要拆?

0

下面的行是错误的:

name[i] = cityName.get(0).toString().split(""); 

(如异常消息可能指示)。事实上,你试图初始化name[i],这是一个字符串,结果为split方法,它返回一个字符串数组。

0

我对Android没有任何经验,所以我不知道这些方法应该返回什么,但是String.split会返回一个String数组。名字[我]只是一个字符串。所以你试图给一个简单的String赋值一个String数组。

0

Split方法返回一个String数组;所以,当你这样做:

name[i] = cityName.get(0).toString().split(""); 

它会返回一个字符串数组其中将包含所有的字符串的字母,

相关问题