我正在我的应用程序中实现ExpandableListView,而且我遇到了一个我无法弄清楚的Null指针。实现可扩展列表对我来说很棘手,所以我希望有比我更多经验的人可以看到我的错误。日志说NPE是由我的Activity中的第55行引起的。作为参考,那就是调用listView.setAdapter(listAdapter);
的行。这是我的活动:
public class SearchResultsActivity extends ExpandableListActivity {
private static final String TAG = SearchResultsActivity.class.getSimpleName();
private ExpandableListView listView;
private SearchItemAdapter listAdapter;
private List<SearchItem> searchItems;
private List<InventoryItem> inventoryItems;
private SharedPreferences mPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
//define listivew and data model
listView = (ExpandableListView) findViewById(R.id.list);
searchItems = new ArrayList<SearchItem>();
listAdapter = new SearchItemAdapter(this, searchItems);
listView.setAdapter(listAdapter);
handleIntent(getIntent());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_results, menu);
return true;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// handle search intent
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
volleySearch(query);
}
}
private void parseJsonFeed(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject searchObj = (JSONObject) response.get(i);
SearchItem searchItem = new SearchItem();
searchItem.setId(searchObj.getInt("id"));
// product
searchItem.setProduct(searchObj.getString("product"));
//image
searchItem.setItemImg(searchObj.getString("img"));
//id
searchItem.setId(searchObj.getInt("id"));
//upc
searchItem.setUpc(searchObj.getInt("upc"));
// inventory_items
JSONArray array = searchObj.getJSONArray("inventory_item");
for (int k = 0; k < array.length(); k++ ) {
JSONObject invItem = (JSONObject) array.get(k);
InventoryItem item = new InventoryItem();
item.setId(invItem.getInt("id"));
item.setStoreName(invItem.getString("name"));
inventoryItems.add(item);
}
searchItems.add(searchItem);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
volleySearch
只是发出一个Volley请求,然后调用parseJsonFeed
。
这是我的适配器:
public class SearchItemAdapter extends BaseExpandableListAdapter {
private Activity activity;
private Context context;
private List<SearchItem> searchItems;
private LayoutInflater inflater;
public SearchItemAdapter(Context context, List<SearchItem> searchItems) {
this.context = context;
this.searchItems = searchItems;
inflater = LayoutInflater.from(context);
}
@Override public Object getChild(int groupPosition, int childPosition) {
return this.searchItems.get(groupPosition).getInventoryItems().get(childPosition);
}
@Override public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.searchItems.get(groupPosition).getInventoryItems()
.size();
}
@Override public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final InventoryItem item = (InventoryItem) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.inventory_item, null);
}
TextView storeName = (TextView) convertView.findViewById(R.id.store_name);
storeName.setText(item.getStoreName());
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
SearchItem item = (SearchItem) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.search_item, null);
}
TextView product = (TextView) convertView
.findViewById(R.id.product);
product.setText(item.getProduct());
return convertView;
}
@Override
public Object getGroup(int groupPosition) {
return this.searchItems.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.searchItems.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return false;
}
}
最后我的数据对象:
SearchItem
public class SearchItem {
private int id;
private int upc;
private String product;
private String itemImg;
private List<InventoryItem> inventoryItems;
public SearchItem() {
}
public SearchItem(int id, int upc, String product, String itemImg) {
super();
this.id = id;
this.product = product;
this.itemImg = itemImg;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUpc() {
return upc;
}
public void setUpc(int upc) {
this.upc = upc;
}
public String getItemImg() {
return itemImg;
}
public void setItemImg(String itemImg) {
this.itemImg = itemImg;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public List<InventoryItem> getInventoryItems() {
return inventoryItems;
}
public void setInventoryItems(List<InventoryItem> inventoryItems) {
this.inventoryItems = inventoryItems;
}
}
InventoryItem
public class InventoryItem {
private int id;
private int upc;
private String vendorImage;
private String storeName;
private String price;
public InventoryItem() {
}
public InventoryItem(int id, int upc, String storeName, String vendorImage, String price) {
super();
this.id = id;
this.upc = upc;
this.vendorImage = vendorImage;
this.storeName = storeName;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUpc() {
return upc;
}
public void setUpc(int upc) {
this.upc = upc;
}
public String getVendorImage() {
return vendorImage;
}
public void setVendorImage(String vendorImage) {
this.vendorImage = vendorImage;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
感谢您对此有任何帮助或反馈!
答案 0 :(得分:1)
问题在于这一行:
listView = (ExpandableListView) findViewById(R.id.list);
使用ExpandableListActivity
中的函数getExpandableListViewlistView = getExpandableListView();