2012-09-28 89 views
0

好吧我试图下载一些XML并使用碎片。我有一个来源未找到错误,并相信有我的代码的东西。该logcat的是不产生错误的应用程序,只要它是在这里打开关闭是我的主要片段Android碎片问题

package com.paad.earthquake; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.GregorianCalendar; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

import android.app.ListFragment; 
import android.location.Location; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ArrayAdapter; 

public class EarthquakeListFragment extends ListFragment { 

ArrayAdapter<Quake> aa; 
ArrayList<Quake> earthquakes = new ArrayList<Quake>(); 

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
super.onActivityCreated(savedInstanceState); 

int layoutID = android.R.layout.simple_list_item_1; 
aa = new ArrayAdapter<Quake>(getActivity(), layoutID , earthquakes); 
setListAdapter(aa); 

refreshEarthquakes(); 
    } 

    private static final String TAG = "EARTHQUAKE"; 
    private void refreshEarthquakes() { 
    // Get the XML 
    URL url; 
    try { 
     String quakeFeed = getString(R.string.quake_feed); 
     url = new URL(quakeFeed); 

    URLConnection connection; 
    connection = url.openConnection(); 

    HttpURLConnection httpConnection = (HttpURLConnection)connection; 
    int responseCode = httpConnection.getResponseCode(); 

    if (responseCode == HttpURLConnection.HTTP_OK) { 
    InputStream in = httpConnection.getInputStream(); 

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 

    // Parse the earthquake feed. 
    Document dom = db.parse(in); 
    Element docEle = dom.getDocumentElement(); 

    // Clear the old earthquakes 
    earthquakes.clear(); 

    // Get a list of each earthquake entry. 
    NodeList nl = docEle.getElementsByTagName("entry"); 
    if (nl != null && nl.getLength() > 0) { 
     for (int i = 0 ; i < nl.getLength(); i++) { 
     Element entry = (Element)nl.item(i); 
     Element title = (Element)entry.getElementsByTagName("title").item(0); 
     Element g = (Element)entry.getElementsByTagName("georss:point").item(0); 
     Element when = (Element)entry.getElementsByTagName("updated").item(0); 
     Element link = (Element)entry.getElementsByTagName("link").item(0); 

     String details = title.getFirstChild().getNodeValue(); 
     String hostname = "http://earthquake.usgs.gov"; 
     String linkString = hostname + link.getAttribute("href"); 

     String point = g.getFirstChild().getNodeValue(); 
     String dt = when.getFirstChild().getNodeValue(); 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); 
     Date qdate = new GregorianCalendar(0,0,0).getTime(); 
     try { 
      qdate = sdf.parse(dt); 
     } catch (ParseException e) { 
      Log.d(TAG, "Date parsing exception.", e); 
     } 

     String[] location = point.split(" "); 
     Location l = new Location("dummyGPS"); 
     l.setLatitude(Double.parseDouble(location[0])); 
     l.setLongitude(Double.parseDouble(location[1])); 

     String magnitudeString = details.split(" ")[1]; 
     int end = magnitudeString.length()-1; 
     double magnitude = Double.parseDouble(magnitudeString.substring(0, end)); 

     details = details.split(",")[1].trim(); 

     Quake quake = new Quake(qdate, details, l, magnitude, linkString); 

     // Process a newly found earthquake 
      addNewQuake(quake); 
      } 
     } 
     } 
    } catch (MalformedURLException e) { 
     Log.d(TAG, "MalformedURLException", e); 
    } catch (IOException e) { 
     Log.d(TAG, "IOException", e); 
    } catch (ParserConfigurationException e) { 
    Log.d(TAG, "Parser Configuration Exception", e); 
} catch (SAXException e) { 
    Log.d(TAG, "SAX Exception", e); 
} 
finally { 
} 
    } 

    private void addNewQuake(Quake _quake) { 
    // Add the new quake to our list of earthquakes. 
     earthquakes.add(_quake); 

     // Notify the array adapter of a change. 
     aa.notifyDataSetChanged(); 
     } 

这是我主要的Java类

package com.paad.earthquake; 

    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 

    public class Earthquake extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    } 

} 

这里是我的主XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 
<fragment 
    android:name="com.paad.earthquake.EarthquakeListFragment" 
    android:id="@+id/EarthquakeListFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

    </LinearLayout> 

我也有一个帮手类,但我不认为你必须看到这一点。任何帮助表示赞赏。日志猫没有抛出异常,甚至不知道从哪里开始寻找这一个。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.paad.earthquake" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".Earthquake" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+0

您是否添加了支持库? –

+0

我对支持库的含义略有困惑。我已经从谷歌下载了所有的附件。有什么特别的碎片? – blankwall

+0

你在一个软糖设备上测试吗? – midhunhk

回答

1

在我的经验,推出后直接关闭没有一个堆栈跟踪通常意味着有一个与你的AndroidManifest.xml文件有问题。你在清单中声明了你的主要活动吗?

如果你这样做,你可以在这里发布?

+0

我张贴在底部 – blankwall

+0

嗯..在那里没有问题的清单。你能发布实际的错误信息吗? – digidigo

+0

它没有抛出任何错误,这就是为什么它如此奇怪。日志猫不说任何我只是得到臭名昭着的来源未找到附加一个来源。也许我错过了片段所需的某些源代码。我试过它在我的phon上,这是api 10和模拟器上这是api16 – blankwall