java - 无法从 String[] 转换为 String

标签 java android 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]"

我试图将它转换为字符串并将其拆分。我制作了一个字符串数组,但是当我尝试将“cityName”分配给字符串数组“name”时,我收到此错误 - “类型不匹配:无法从字符串 [] 转换到字符串”

你能告诉我我哪里错了吗?

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();
    }
}

}

最佳答案

这里的所有答案都很好。 但我想知道的是,为什么要对空字符使用拆分?

你知道它返回单个字符的数组(开始时什么也没有)吗?

检查代码:

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();

希望这对您有所帮助。

关于java - 无法从 String[] 转换为 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136914/

相关文章:

android - RadioGroupbuttons 在 listView 滚动后失去其状态

java - 如何动态追加字符串?

javascript - 如何在 javascript 中将字符串变量传递给 String.raw?

Java:从我的应用程序按需运行 HTTP 服务器?

Java 比较数组中的字符串

Java ME - 寻求教程

java - 设置WebView大小

android - Jenkins:致命:无法初始化类 hudson.util.ProcessTree$UnixReflection

javascript - 如何根据从字符串中提取的名称在运行时创建变量?

java - 获取 NoSuchMethodError :javax. servlet.ServletContext.getVirtualServerName()