0

我正在使用谷歌地图版本 2 开发一个 andorid 应用程序。我使用 DOM 读取了一个 xml 文件以获取我需要的数据。xml 解析一切都很好(我从 logcat 中看到)。我将读取的数据放入 Hotel 类型的 ArrayList(我的一类)。当我尝试使用 lat 和 lng 信息放置标记时,我读取的应用程序停止了。但它仅在我读入循环时才停止,而不是当我只读一个时(例如列表中添加的最后一个)。我尝试将列表缩短到一半,但它也不起作用。

这是代码:Hoteli.java:

public class Hoteli extends FragmentActivity {
GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hoteli);
    setTitle("Хотели");

    ArrayList<Hotel> hoteli= new ArrayList<Hotel>();
    Hotel h=null;

    Log.d("", "pred try delot");
    try {
        InputStream in = this.getResources().openRawResource(R.raw.hoteli);
        StringBuffer str = new StringBuffer();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(in, null);
        document.getDocumentElement().normalize();

        Log.d("", "Root element :"
                + document.getDocumentElement().getNodeName());

        NodeList nList = document.getElementsByTagName("hotel");

        Log.d("", "----------------------------");
        Log.d("",Integer.toString(nList.getLength()));
        for (int temp = 0; temp < nList.getLength(); temp++) {

            h= new Hotel();
            Node nNode = nList.item(temp);

            Log.d("", "\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                Log.d("", "Staff hotel : " + eElement.getAttribute("hotel"));
                h= new Hotel();
                Log.d("", "Name: "
                        + eElement.getElementsByTagName("name").item(0)
                                .getTextContent());
                h.setName(eElement.getElementsByTagName("name").item(0)
                                .getTextContent());
                Log.d("", "City : "
                        + eElement.getElementsByTagName("city").item(0)
                                .getTextContent());
                h.setCity(eElement.getElementsByTagName("city").item(0)
                                .getTextContent());
                Log.d("", "Lat : "
                        + eElement.getElementsByTagName("lat").item(0)
                                .getTextContent());
                h.setLat(eElement.getElementsByTagName("lat").item(0)
                                .getTextContent());
                Log.d("", "Lng : "
                        + eElement.getElementsByTagName("lng").item(0)
                                .getTextContent());
                h.setLng( eElement.getElementsByTagName("lng").item(0)
                                .getTextContent());
                Log.d("",
                        "Stars : "
                                + eElement.getElementsByTagName("stars")
                                        .item(0).getTextContent());
                h.setStars(eElement.getElementsByTagName("stars")
                                        .item(0).getTextContent());
                Log.d("",
                        "Description : "
                                + eElement
                                        .getElementsByTagName("desciption")
                                        .item(0).getTextContent());
                h.setDescription(eElement
                                        .getElementsByTagName("desciption")
                                        .item(0).getTextContent());
                hoteli.add(h);

            }
        }

    } catch (Exception err) {
        err.printStackTrace();
    }

    map = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapaHoteli)).getMap();
int halfLength= hoteli.size()/2;
    for(int i=0;i<halfLength;i++)
    {
     final LatLng   currentPositionLatLng=new LatLng(Double.parseDouble(hoteli.get(i).getLat()), Double.parseDouble(hoteli.get(i).getLng()));
    Marker currentPosition = map.addMarker(new MarkerOptions()
              .position(currentPositionLatLng)
              .title("\"" +hoteli.get(i).getName()+"\""+ " "+ hoteli.get(i).getStars()+ " ѕвезди")
              .snippet(hoteli.get(i).getCity() + hoteli.get(i).getDescription())
              .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));

}}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.hoteli, menu);
    return true;
}
}

酒店.java:

public class Hotel {
String name;
String city;
String lat;
String lng;
String stars;
String description;

public Hotel()
{}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getLat() {
    return lat;
}
public void setLat(String lat) {
    this.lat = lat;
}
public String getLng() {
    return lng;
}
public void setLng(String lng) {
    this.lng = lng;
}
public String getStars() {
    return stars;
}
public void setStars(String stars) {
    this.stars = stars;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
}

来自 logcat 的错误:

05-25 22:17:56.937: E/AndroidRuntime(18639): FATAL EXCEPTION: main
05-25 22:17:56.937: E/AndroidRuntime(18639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.probna/com.example.probna.Hoteli}: java.lang.NumberFormatException
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1670)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1686)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.app.ActivityThread.access$1500(ActivityThread.java:118)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:932)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.os.Looper.loop(Looper.java:130)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.app.ActivityThread.main(ActivityThread.java:3706)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at java.lang.reflect.Method.invoke(Method.java:507)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at dalvik.system.NativeStart.main(Native Method)
05-25 22:17:56.937: E/AndroidRuntime(18639): Caused by: java.lang.NumberFormatException
05-25 22:17:56.937: E/AndroidRuntime(18639):    at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at java.lang.Double.parseDouble(Double.java:318)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at com.example.probna.Hoteli.onCreate(Hoteli.java:131)
   05-25 22:17:56.937: E/AndroidRuntime(18639):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 22:17:56.937: E/AndroidRuntime(18639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1634)
05-25 22:17:56.937: E/AndroidRuntime(18639):    ... 11 more
4

1 回答 1

2

解析时hoteli.get(i).getLat()出错hoteli.get(i).getLng()

可能您在此方法中有空字符串编号,或者“,”而不是“。” 作为数字分隔符。

于 2013-05-25T21:02:38.333 回答