我正在開發一個使用谷歌地圖版本2的andorid應用程序。我使用DOM讀取了一個xml文件以獲取我需要的數據。一切都很好用XML解析(我看到從logcat)。讀入的數據放入Hotel類型的ArrayList(我的一個類)。當我嘗試使用lat和lng信息來放置標記時,我讀取了我的應用程序停止。但是,只有當我讀入循環時纔會停止,而不是當我只讀取一個(例如最後一個添加到列表中)。我試圖將名單縮短爲一半,但它不工作。Android應用程序意外停止
下面是代碼: 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:
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
您應該發佈logcat消息,以便我們可以更好地幫助您 –
您可以記錄hoteli.get(i).getLat()和hoteli.get(i).getLng()的值嗎? – ArturSkowronski
是的,我可以,並且我得到每個項目的正確值。下面是在XML一個項目: ' ХотелАлександрија ' 'Охрид ' ' 41.112307 ' ' 20.797965 ' '\t \t \t \t ' –
Katerina