Android的setlistAdapter错误 [英] Android setlistAdapter error
问题描述
喜刚得到一个错误,我不知道为什么谁能帮助?
当我运行它,它会强制关闭应用程序。不知道这意味着什么,因为我是新的,但希望有人能帮忙吗?
继承人从LogCat中的误差
这是我的JSON饲料的结构是怎样,我想这个消息阵列
{code:200,错误:空,数据:{新闻:[{news_id:8086
我在这里得到一个错误:在oneOjectsItem
setListAdapter(新ArrayAdapter(这一点,R.layout.single_item,oneObjectsItem));
在这里我的code
//从请求响应实例化一个JSON对象
的JSONObject的JSONObject =新的JSONObject(JSON); JSONArray jArray = jsonObject.getJSONArray(新闻);
的for(int i = 0; I< jArray.length();我++)
{
JSONObject的oneObject = jArray.getJSONObject(I)
//从阵列中拉项目
字符串oneObjectsItem = oneObject.getString(标题); }
}赶上(例外五){
//在生产code处理任何错误,赶上个别例外
e.printStackTrace();
}
setListAdapter(新ArrayAdapter<串GT;(这一点,R.layout.single_item,oneObjectsItem));ListView控件列表= getListView();
list.setTextFilterEnabled(真);
list.setOnItemClickListener(新OnItemClickListener(){ 公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
// TODO自动生成方法存根
Toast.makeText(getApplicationContext(),((TextView的)ARG1).getText(),1000).show();
}});}
我猜测你有字符串oneObjectsItem
未初始化的另一个声明。然后创建(误?)在另一个字符串
循环,并初始化它,而第一个被保留为空
。
如果我理解正确你的逻辑可能是这样的:
列表<串GT;标题=新的ArrayList<串GT;(); 的for(int i = 0; I< jArray.length();我++){
oneObject = jArray.getJSONObject(ⅰ);
//从阵列中拉项目
titles.add(oneObject.getString(标题));
}setListAdapter(新ArrayAdapter<串GT;(这一点,R.layout.single_item,标题));
hi just getting an error and I'm not sure why can anyone help?
when i run it it forces the application to close. not sure what this means as i'm new but hopefully someone can help?
heres the error from the LogCat
this is how my json feed is structured i want the news array
{"code":200,"error":null,"data":{"news":[{"news_id":"8086" I'm getting an error here: in the oneOjectsItem
setListAdapter ( new ArrayAdapter(this, R.layout.single_item, oneObjectsItem));
here my code
// 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();
}
});
}
I am guessing that you have another declaration of String oneObjectsItem
which is not initialized. Then you create (by mistake?) another string inside for
loop and init it, while the first one is left as null
.
If I understand your logic correctly it could be something like this:
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));
这篇关于Android的setlistAdapter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!