我也是 Android 和 Java 的新手。我来自 PHP。以下代码摘自一本书(Pro Android 4 App Dev - Reto Meier): 这是我遇到问题的 Android 代码:
文件:EarthquakeListFragment.java
public class EarthquakeListFragment extends ListFragment {
ArrayAdapter<Quake> aa;
ArrayList<Quake> earthquakes = new ArrayList<Quake>();
private static final String TAG = "EARTHQUAKE";
private Handler handler = new Handler();
@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);
Thread t = new Thread(new Runnable() {
public void run() {
refreshEarthquakes();
}
});
t.start();
}
public 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 earthquake
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();
final Quake quake = new Quake(qdate, details, l,
magnitude, linkString);
// Process a newly found earthquake
handler.post(new Runnable() {
public void run() {
addNewQuake(quake);
}
});
}
}
}
}
catch (HttpHostConnectException e) {
Log.d(TAG, "HttpConnection Error.");
}
catch (MalformedURLException e) {
Log.d(TAG, "MalformedURLException");
} catch (IOException e) {
Log.d(TAG, "IOException");
} catch (ParserConfigurationException e) {
Log.d(TAG, "Parser Config Exception");
} catch (SAXException e) {
Log.d(TAG, "SAX Exception");
} finally {
}
}
private void addNewQuake(Quake _quake) {
// add the new quake to our list of earthquake
earthquakes.add(_quake);
// Notify the array adapter of a change
aa.notifyDataSetChanged();
}
}
这是 LogCat 的一部分,其中显示错误:
09-09 20:03:15.689: W/dalvikvm(1095): threadid=11: 线程以未捕获的异常退出 (group=0x40a71930) 09-09 20:03:15.735: E/AndroidRuntime(1095): 致命异常: 线程-111 09-09 20:03:15.735:E/AndroidRuntime(1095):java.lang.NullPointerException 09-09 20:03:15.735:E/AndroidRuntime(1095):在 com.satyaweblog.earthquake.EarthquakeListFragment.refreshEarthquakes( EarthquakeListFragment.java:106) 09-09 20:03:15.735: E/AndroidRuntime(1095): at com.satyaweblog.earthquake.EarthquakeListFragment$1.run(EarthquakeListFragment.java:50) 09-09 20:03:15.735: E /AndroidRuntime(1095): at java.lang.Thread.run(Thread.java:856) 09-09 20:03:20.015: I/Process(1095): 发送信号。PID:1095 SIG:9
因此,在一处,代码行是:
String point = g.getFirstChild().getNodeValue();
在 PHP 中,我会这样检查: var_dump(g); 如何在 Android + Java 中查看这里?
我认为,第二个错误将是由于第一个错误!