任何人都可以帮我弄清楚为什么我在这里得到NullPointerException? 我正在尝试为ExpandableListViewActivity设置适配器。
在下面的代码中,我知道我的数组正在填充对象,但是当我尝试从适配器的getGroupView()方法中的数组中检索对象时,我得到一个nullpointerexception。
有趣的是,我可以在错误发生之前在代码行中获取对象的name属性。
public class Section extends ExpandableListActivity {
bpsection[] sectionsArray;
bpcategory[][] categoriesArray;
ExpandableListAdapter sectionsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_section);
LoadCategoriesTask loadingtask = new LoadCategoriesTask();
loadingtask.execute();
}
private class bpsection {
public int id;
public String name;
public int isadult;
public bpsection(int _id, String _name, int _isadult) {
this.id = _id;
this.name = _name;
this.isadult = _isadult;
}
}
private class bpcategory extends bpsection {
@SuppressWarnings("unused")
public int section;
public bpcategory(int _section, int _id, String _name, int _isadult) {
super(_id, _name, _isadult);
this.section = _section;
}
}
/*
* CUSTOM ADAPTER
*/
public class BPSectionsAdapter extends BaseExpandableListAdapter {
private LayoutInflater blowMeUp;
public BPSectionsAdapter(Context context)
{
blowMeUp = LayoutInflater.from(context);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return categoriesArray[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return categoriesArray[groupPosition][childPosition].id;
}
@Override
public int getChildrenCount(int groupPosition) {
return categoriesArray[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return sectionsArray[groupPosition];
}
@Override
public int getGroupCount() {
return sectionsArray.length;
}
@Override
public long getGroupId(int groupPosition) {
return sectionsArray[groupPosition].id;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View parentView = blowMeUp.inflate(R.layout.itm_section, null);
TextView tv = (TextView) parentView.findViewById(R.id.sectionView);
Log.v("BPC", "Getting Group View for Section " + ((bpsection)getGroup(groupPosition)).name);
if(tv == null)Log.v("BPC", "Text view isnull");
if(((bpsection)getGroup(groupPosition)) == null)Log.v("BPC", "section is null");
tv.setText(((bpsection)getGroup(groupPosition)).name);
return parentView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View childView = convertView;
if (childView == null) {
childView = blowMeUp.inflate(R.layout.itm_category, null);
}
TextView tv = (TextView) childView.findViewById(R.id.categoryView);
tv.setText(((bpcategory) getChild(groupPosition, childPosition)).name);
return childView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
/*
* ASYNC TASK
*/
private class LoadCategoriesTask extends AsyncTask<Void, Void, Void> {
private BPCDB db;
@Override
protected void onPostExecute(Void result) {
sectionsAdapter = new BPSectionsAdapter(Section.this);
setListAdapter(sectionsAdapter);
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<bpcategory> categories;
this.db = new BPCDB(getApplicationContext());
db.open();
Cursor c_section = db.getSections();
if(c_section.getCount() > 1)
try {
db.dbhelper.copyDBFromAssets();
c_section = db.getSections();
} catch (IOException e) {
e.printStackTrace();
}
sectionsArray = new bpsection[c_section.getCount()]; // We can init
// now that
// we have a
// count
categoriesArray = new bpcategory[c_section.getCount()][];
int i = 0;
for (c_section.moveToFirst(); c_section.moveToNext(); c_section
.isAfterLast()) {
bpsection s = new bpsection(c_section.getInt(0), // id
c_section.getString(1), // name
c_section.getInt(2) // isadult
);
sectionsArray[i] = s;
categories = new ArrayList<bpcategory>(); // Reinitialize
// category list for
// each section
// iteration
Cursor c_category = db.getCategories(Integer.toString(s.id));
// The first category of each section will be the "all whatever"
// of the section
categories.add(new bpcategory(s.id, 0, "all " + s.name,
s.isadult));
for (c_category.moveToFirst(); c_category.moveToNext(); c_category
.isAfterLast()) {
bpcategory c = new bpcategory(c_category.getInt(1),
c_category.getInt(0), c_category.getString(2),
c_category.getInt(3));
categories.add(c);
} // end of categories loop
int size = categories.size();
Log.v("BPC", "Section: " + s.name + "Categories: "+ size);
categoriesArray[i] = categories.toArray(new bpcategory[size]);
} // end of section loop
Log.v("BPC", "Sections: " + sectionsArray.length);
Log.v("BPC", "Categories: " + categoriesArray.length);
return null;
}
}
}
这是LogCat
07-13 06:27:57.719: VERBOSE/BPC(210): Getting Group View for Section services
07-13 06:27:57.719: VERBOSE/BPC(210): section is null
07-13 06:27:57.729: DEBUG/AndroidRuntime(210): Shutting down VM
07-13 06:27:57.729: WARN/dalvikvm(210): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
07-13 06:27:57.729: ERROR/AndroidRuntime(210): Uncaught handler: thread main exiting due to uncaught exception
07-13 06:27:57.749: ERROR/AndroidRuntime(210): java.lang.NullPointerException
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at com.mogulsoftware.android.bpcruiser.Section$BPSectionsAdapter.getGroupView(Section.java:117)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.AbsListView.obtainView(AbsListView.java:1273)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.ListView.makeAndAddView(ListView.java:1658)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.ListView.fillDown(ListView.java:637)
提示错误(117)的行是:
tv.setText(((bpsection)getGroup(groupPosition)).name);
感谢任何帮助。
答案 0 :(得分:0)
回顾你的日志:
07-13 06:27:57.719: VERBOSE/BPC(210): section is null
第115行编码:
if(((bpsection)getGroup(groupPosition)) == null)Log.v("BPC", "section is null");
然后第117行引发NullPointerException,因为
getGroup(groupPosition)
null
,无法访问属性name
答案 1 :(得分:0)
当您填充剖面阵列时,请查看您的循环。看起来i
不会增加。也许更准确地解释for(...)
中的陈述
int i = 0;
for (c_section.moveToFirst(); c_section.moveToNext(); c_section
.isAfterLast()) {
bpsection s = new bpsection(c_section.getInt(0), // id
c_section.getString(1), // name
c_section.getInt(2) // isadult
);
sectionsArray[i] = s;
...
}