0

我有这段代码,它必须获取一个列表视图并用数组适配器填充它:

public class news extends ListActivity {
public String[] titoli = new String[99];
public String[] descrizioni = new String[99];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new connection().execute();
    System.out.println("prova1");
}

public class connection extends AsyncTask<Void, Void, Void> {
    public Void doInBackground(Void... params) {
        System.out.println("prova2");
        // All static variables
        final String URL = "http://www.messedaglia.it/index.php/archivio-news?format=feed&type=rss";
        // XML node keys
        final String ITEM = "item"; // parent node
        final String TITLE = "title";
        final String DESC = "description";
        Element e = null;
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(ITEM);

        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(TITLE, parser.getValue(e, TITLE));
            map.put(DESC, parser.getValue(e, DESC));

            // adding HashList to ArrayList
            menuItems.add(map);
        }
        System.out.println("prova3");
        for (int c = 0; c < nl.getLength(); c++) {
            e = (Element) nl.item(c);
            titoli[c] = parser.getValue(e, TITLE); // name child value
            descrizioni[c] = parser.getValue(e, DESC);
        }
        return null;
    }

    public void onPostExecute() {
        System.out.println("prova4");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(news.this,
                android.R.layout.simple_list_item_1, titoli);
        System.out.println("prova5");
        ListView listView = (ListView) news.this
                .findViewById(android.R.id.list);
        listView.setAdapter(adapter);
    }
     }
 }

日志在此处显示空指针异常(我的代码的第 33 行):

listView.setAdapter(adapter);

日志:

02-07 13:38:36.020: E/AndroidRuntime(13803): FATAL EXCEPTION: main
02-07 13:38:36.020: E/AndroidRuntime(13803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.messedagliavr.messeapp/com.messedagliavr.messeapp.news}: java.lang.NullPointerException
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.os.Looper.loop(Looper.java:137)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at java.lang.reflect.Method.invoke(Method.java:511)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at dalvik.system.NativeStart.main(Native Method)
02-07 13:38:36.020: E/AndroidRuntime(13803): Caused by: java.lang.NullPointerException
02-07 13:38:36.020: E/AndroidRuntime(13803):    at com.messedagliavr.messeapp.news.onCreate(news.java:33)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.Activity.performCreate(Activity.java:5206)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
02-07 13:38:36.020: E/AndroidRuntime(13803):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
02-07 13:38:36.020: E/AndroidRuntime(13803):    ... 11 more
    

大多数人说要添加 setContentView()。我删除了它,因为添加它我得到这个错误:

错误:java.lang.RuntimeException:您的内容必须有一个 id 属性为“android.R.id.list”的 ListView

4

3 回答 3

2

您缺少设置主布局

     public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
     setContentView(R.layout.main);    <----------------here
     new connection().execute();
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, titoli);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
于 2013-02-07T12:50:09.243 回答
2

如日志:

NullPointerException 02-07 13:38:36.020:E/AndroidRuntime(13803):
在 com.messedagliavr.messeapp.news.onCreate(news.java:33)

因为titoli数组是空的。您正在使用 AsyncTask 获取titoli数组中的元素,然后只需移动您的适配器代码onPostExecute方法

并使用

ListView listView = (ListView) findViewById(android.R.id.list);

代替

ListView listView = (ListView) findViewById(R.id.list);

获取参考, 如果您正在使用,则必须在 xml 布局中ListView声明 ListView idandroid:id="@android:id/list"

于 2013-02-07T12:51:42.063 回答
1

这是因为您的组件上没有设置布局。首先你需要打电话setContentView(R.layout.my_great_layout),然后打电话才有意义findViewById

确保你的布局中有一个ListViewwith android.R.id.listid:

  <ListView
      android:id="@android:id/list"
  .....
于 2013-02-07T12:49:52.837 回答