0

我在 Android 应用程序中遇到了一些问题:我想从 Feed RSS 中读取信息并将它们呈现在一些 android 视图中。我正在使用android-async-http1.4.3.jar库将我的应用程序连接到该站点:http ://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://f1grandprix.motorionline.com/feed /

通过此连接,我正在下载 XML 文件并将其转换为带有 gson 库的 JSON 文件,但是当我尝试将 APP 连接到我的 Android 手机时,我得到了一个空指针异常。用于连接、获取 xml 并将其转换为 JSON 代码的类如下:

    private void loadTitle() {
    String URL = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://f1grandprix.motorionline.com/feed/";

    AsyncHttpClient client = new AsyncHttpClient();

    client.get(URL, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String text) {
            super.onSuccess(text);

            Gson decoder = new Gson();

            JsonResponse jr = decoder.fromJson(text, JsonResponse.class);

            Entry[] entries = jr.responseData.feed.entries;
            for (int i = 0; i < entries.length; i++) {
                Log.d("NET", "Il titolo della " + i + " entry è: " + entries[i].title);
            }


            final ListView lv_title = (ListView) findViewById(R.id.lv_title);
            //ListAdapter la_title = new TitleAdapter(MainActivity.this, entries);
            //Log.d("LIST_ADAPTER", "Caricato correttamente il list adaptor: "+ la_title);
            //lv_title.setAdapter(la_title);
            lv_title.setAdapter(new TitleAdapter(MainActivity.this, entries));
            pleaseWait.hide();
        }

        @Override
        public void onFailure(Throwable arg0, String arg1) {
            super.onFailure(arg0, arg1);
            Log.d("FAIL", "Failure");
            pleaseWait.hide();
        }
    });
}

TitleAdapter.java 如下:

public class TitleAdapter extends ArrayAdapter<Entry> {

public TitleAdapter(Context context, Entry[] title) {
    super(context, R.layout.list_item, R.id.lv_title, title);
}

}

最后我收到了这个错误:

02-23 12:46:05.525: E/AndroidRuntime(4577): FATAL EXCEPTION: main
02-23 12:46:05.525: E/AndroidRuntime(4577): java.lang.NullPointerException
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.AbsListView.obtainView(AbsListView.java:1495)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.ListView.onMeasure(ListView.java:1127)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.View.measure(View.java:8335)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.View.measure(View.java:8335)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.View.measure(View.java:8335)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.View.measure(View.java:8335)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.View.measure(View.java:8335)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1892)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.os.Looper.loop(Looper.java:130)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at android.app.ActivityThread.main(ActivityThread.java:3835)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at java.lang.reflect.Method.invoke(Method.java:507)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
02-23 12:46:05.525: E/AndroidRuntime(4577):     at dalvik.system.NativeStart.main(Native Method)

有人可以帮我解决这个问题吗?谢谢!

4

1 回答 1

0

有人可以帮我解决这个问题吗?

entries您传递给自定义的数组中ArrayAdapter(您仍然为其调用super.getView()方法(或者您没有覆盖它),对吗?)看起来您有null值。如果您使用ArrayAdapter'getView()方法的默认实现,这是一个问题,因为ArrayAdapter将在该方法中调用,默认情况下,toString()其类型对象(Entry在您的情况下为对象)上的方法以获得String在行中显示的 a。

因此,请确保您传递给您的实现的数组ArrayAdapter 包含空值。

于 2013-02-23T15:41:48.970 回答