【发布时间】:2014-05-10 20:14:04
【问题描述】:
我正在尝试使用 ExpandableListAdapter 和 followed this tutorial 编写应用程序,但我将使用 json 对我的数据库 (MYSQL) 进行整数运算,所以我遇到了这个问题:
Caused by: java.lang.NullPointerException at
com.example.smart_survey.ExpandableListAdapter.getGroupCount
(ExpandableListAdapter.java:71)
有人可以帮帮我吗
班级ExpandableListAdapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
**private List<String> _listDataHeader= null;** // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
**this._listDataHeader = listDataHeader;**
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
类二维码
public class QR extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
ArrayList<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// url to get all products list
private static String url_all_quests =
"http://10.0.2.2/android_connect/listquest.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_QUEST = "Question";
private static final String TAG_REP = "Reponse";
private static final String TAG_IDE = "idenq";
private static final String TAG_IDQ = "idquest";
private static final String TAG_LIBQ = "libquest";
private static final String TAG_LIBR = "librep";
// products JSONArray
String idenq;
String idquest;
String libquest,librep;
Button valider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_quest);
Intent i = getIntent();
idenq = i.getStringExtra(TAG_IDE);
idquest = i.getStringExtra(TAG_IDQ);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.list);
// preparing list data
new prepareListData().execute();
listAdapter = new ExpandableListAdapter(this, listDataHeader,
listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + "
Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + "
Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+
listDataChild.get(listDataHeader.get(groupPosition)).
get(childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
class prepareListData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("idenq", idenq));
params1.add(new BasicNameValuePair("idquest", idquest));
params1.add(new BasicNameValuePair("libquest", libquest));
params1.add(new BasicNameValuePair("librep", librep));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(
url_all_quests, "GET", params1);
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// products found
// Getting Array of Products
JSONArray Question;
try {
Question = json.getJSONArray(TAG_QUEST);
// looping through All Products
for (int i = 0; i < Question.length(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Storing each json item in variable
JSONObject c=Question.getJSONObject(i);
String libquest = c.getString(TAG_LIBQ);
map.put(TAG_LIBQ, libquest);
// Adding child data
listDataHeader.add(libquest);
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
listDataChild.put(listDataHeader.get(i), top250); // Header, Child data
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
【问题讨论】:
-
可能你没有初始化
_listDataHeader,调试你的程序看看 -
在你的构造函数中,检查你是否真的在
listDataHeader中传递了一个初始化对象,对于这一行this._listDataHeader = listDataHeader; -
我初始化了_listDataHeader,但还是同样的问题
-
你能展示你初始化数据并调用
ExpandableListAdapter的代码片段吗? -
私有上下文_context;私有列表
_listDataHeader=null; // 标题标题 // 标题标题格式的子数据,子标题 private HashMap > _listDataChild; public ExpandableAdapter(上下文上下文,List listDataHeader,HashMap > listChildData){ this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; }
标签: android json expandablelistadapter