Android - 无法使用 JSONArray 数据填充 TableView
我试图用从 MySql 数据库获取的数据填充 TableLayout,我从执行查询并返回 JSONArray 的 php 脚本获取数据。
查询似乎有效,我得到了正确的 JSONArray,但在运行此代码时得到 NullPointerException:
JSONArray jArray = new JSONArray(result);
TableLayout tl = (TableLayout) findViewById(R.layout.list);
TableRow tr = new TableRow(this);
TextView title = new TextView(this);
TextView author = new TextView(this);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
title.setText(json_data.getString("title").toString());
author.setText(json_data.getString("author").toString());
tr.addView(title);
tr.addView(author);
tl.addView(tr);
tr.removeAllViews();
}
“结果”包含有效的 JSONArray,可能在 for 循环中出现问题,但我不明白错误在哪里!
感谢您的帮助!
i'm tryng to populate a TableLayout witth data that i get from a MySql database, i get the data from a php script that execute the query and return a JSONArray.
The query seem to work and i get a correct JSONArray, but i get a NullPointerExeption while running this code:
JSONArray jArray = new JSONArray(result);
TableLayout tl = (TableLayout) findViewById(R.layout.list);
TableRow tr = new TableRow(this);
TextView title = new TextView(this);
TextView author = new TextView(this);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
title.setText(json_data.getString("title").toString());
author.setText(json_data.getString("author").toString());
tr.addView(title);
tr.addView(author);
tl.addView(tr);
tr.removeAllViews();
}
"result" contain a valid JSONArray, probably something go wrong in the for cycle, but i don't understand where is the error!
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论