Android应用程序意外停止

时间:2013-05-25 20:02:14

标签: android google-maps-markers google-maps-api-2

我正在使用谷歌地图版本2开发一个andorid应用程序。我使用DOM读取xml文件以获取我需要的数据。使用xml解析一切都很好(我从logcat中看到)。读取的数据我放入Hotel的类型ListList(我的一类)。当我尝试使用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;
}
}

Hotel.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

1 个答案:

答案 0 :(得分:2)

解析hoteli.get(i).getLat()hoteli.get(i).getLng()

时出错

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