Android setlistAdapter错误

时间:2012-03-08 19:25:27

标签: android

嗨,只是出错,我不确定为什么有人可以帮忙?

当我运行它时会强制应用程序关闭。不知道这意味着什么,因为我是新手,但希望有人可以提供帮助?

来自LogCat enter image description here

的错误

这就是我的json feed的结构,我想要新闻数组

{ “代码”:200, “错误”:NULL, “数据”:{ “新闻”:[{ “news_id”: “8086” 我在这里得到一个错误:在oneOjectsItem

setListAdapter(new ArrayAdapter(this,R.layout.single_item,oneObjectsItem));

这里是我的代码

        // Instantiate a JSON object from the request response
        JSONObject jsonObject = new JSONObject(json);



        JSONArray jArray = jsonObject.getJSONArray("news");


        for (int i=0; i < jArray.length(); i++)
        {
            JSONObject oneObject = jArray.getJSONObject(i);
            // Pulling items from the array
            String oneObjectsItem = oneObject.getString("title");



        }


    } catch(Exception e){
        // In your production code handle any errors and catch the individual exceptions
        e.printStackTrace();
    }




    setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, oneObjectsItem)); 



ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
    }



});

}

1 个答案:

答案 0 :(得分:1)

我猜你还有另一个未初始化String oneObjectsItem的声明。然后你在for循环中创建(错误?)另一个字符串并初始化它,而第一个字符串保留为null

如果我理解你的逻辑,它可能是这样的:

 List<String> titles = new ArrayList<String>();

 for (int i=0; i < jArray.length(); i++){ 
       oneObject = jArray.getJSONObject(i);
        // Pulling items from the array
        titles.add(oneObject.getString("title"));
    }

setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, titles));