-1

私は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(地震リストフラグメント.java:106) 09-09 20:03:15.735: E/AndroidRuntime(1095): com.satyaweblog.earthquake.EarthquakeListFragment$1.run(地震リストフラグメント.java:50) 09-09 20:03:15.735: E /AndroidRuntime(1095): 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でここを確認する方法は?

私は、2番目のエラーは1番目が原因だと思います!

4

2 に答える 2

0

for (int i = 0; i < nl.getLength(); i++)コードをに変更for (int i = 1; i < nl.getLength(); i++)

リンクの xml コードの有効期限が切れているため、機能します。したがって、0 番目のインデックスでは、非推奨のメッセージが表示されます。

i = 1for の代わりにfor ループを開始しますi = 0

于 2014-06-03T18:55:49.513 に答える