我想解析由另一个活动传递的jsonObject
intent.putextra("result", json.toString());它用名称、分支和会话显示所有内容。但它在获取数组时显示NullPointerException。在这里
mSubList.add(map);这个数组有6行。这是我的密码。请准确地告诉我哪里错了。
JSONObject json;
ListView sub_list;
private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mSubList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_page);
name = (TextView)findViewById(R.id.name_roll);
branch= (TextView)findViewById(R.id.branchname);
session = (TextView)findViewById(R.id.Session);
sub_list = (ListView)findViewById(R.id.sub_list);
try {
json = new JSONObject(getIntent().getStringExtra("result"));
name.setText(json.getString("REGNO")+" - "+json.getString("STUDENTNAME"));
branch.setText(json.getString("BRANCHNAME"));
session.setText(json.getString("SESSIONNAME"));
mComments = json.getJSONArray("DATA");
// looping through all subjects according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag
String code = c.getString("CCODE");
String subject = c.getString("COURSENAME");
String passfail = c.getString("PASSFAIL");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("CCODE", code);
map.put("COURSENAME", subject);
map.put("PASSFAIL", passfail);
// adding HashList to ArrayList
mSubList.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mSubList,
R.layout.singlesubject, new String[] { "CCODE", "COURSENAME",
"PASSFAIL" }, new int[] { R.id.sub_id, R.id.sub_name,R.id.sub_res });
sub_list.setAdapter(adapter);
}catch(Exception e){
e.printStackTrace();
}
}
}如果我想改变每一个语言的背景(R.layout.singlesubject)如果"PASSFAIl"=P,那么绿色其他红色。那我要做什么改变?
发布于 2015-01-10 15:25:54
mSubList.add(map);这一行代码失败是因为在添加映射之前没有初始化数组。
你可以用.
mSubList = new ArrayList<Hashmap<String, String>>();发布于 2015-01-10 15:27:46
在添加项目之前插入mSubList
mSubList = new ArrayList<HashMap<String, String>>();发布于 2015-01-10 15:27:53
我认为您在向mSubList添加数据之前忘了初始化它:
mSubList = new ArrayList<HashMap<String, String>>();https://stackoverflow.com/questions/27877779
复制相似问题