我正在尝试在单击父项时在列表视图中创建一个按钮作为子项,然后在单击新父项时从旧父项中删除该按钮并将该按钮添加为子项到新的父项。
尝试从旧父项中删除按钮时似乎出现了主要问题。
以下是之前和之后的示例。
ListView:之前(父母1点击)
父对象1
child1
[按钮]
父对象2
的child2
Listview之后:(父母2点击)
父对象1
child1
父对象2
的child2
[按钮]
单击第一个项目时,动态创建按钮时没有任何问题。但是,在单击新父项时,似乎在从列表视图中删除按钮时出现问题。
以下是我的代码和错误日志。如果需要更多信息,请索取,我将提供完整的信息。
public void initParentListViewListeners(){
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
int lastChild = childArray.get(groupPosition).getNumOfChildren() - 1;
if (lastParentClicked != null && listAdapter.getChild(groupPosition, lastChild) != "Button") {
int size = childArray.get(lastParentPosition).getNumOfChildren();
//Remove button from old ArrayList
childArray.get(lastParentPosition).getChildArray().remove(size - 1);
listAdapter.notifyDataSetChanged();
}
if (listAdapter.getChild(groupPosition, lastChild) != "Button") {
//Add Button to end of ArrayList
childArray.get(groupPosition).getChildArray().add("Button");
lastParentClicked = childArray.get(groupPosition).getChildName();
lastParentPosition = groupPosition;
listAdapter.notifyDataSetChanged();
}
return false;
}
});
}
列表适配器类: 标记是发生错误的地方
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ChildObject> listDataHeader;
public ExpandableListAdapter(Context context, ArrayList<ChildObject> listDataHeader){
this.context = context;
this.listDataHeader = listDataHeader;
}
@Override
public String getChild(int groupPosition, int childPosition){
ArrayList<String> setList = listDataHeader.get(groupPosition).getChildArray();
return setList.get(childPosition);
}
@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){
String childText = (String)getChild(groupPosition, childPosition);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(getChild(groupPosition, childPosition) == "Button"){
convertView = inflater.inflate(R.layout.parent_object_add_child, null);
}
else{
convertView = inflater.inflate(R.layout.parent_item_child, null);
}
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.childObjContents);
/******** Where error is happening ***********/
if(getChild(groupPosition, childPosition) != "Button"){
Log.d(getClass().getSimpleName(), getChild(groupPosition, childPosition));
txtListChild.setText(childText);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition){
ArrayList<String> setList = listDataHeader.get(groupPosition).getChildArray();
return setList.size();
}
@Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount(){
return listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition){
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){
String exerciseTitle = listDataHeader.get(groupPosition).getChildName();
if(convertView == null){
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parent_item_header, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.parentListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(exerciseTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPostition) {
return true;
}
}
错误日志:
11-15 22:03:52.896 2651-2651/myapplication.android.commonsware.com.removelistviewitem E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: myapplication.android.commonsware.com.removelistviewitem, PID: 2651
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at myapplication.android.commonsware.com.removelistviewitem.ExpandableListAdapter.getChildView(ExpandableListAdapter.java:54)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillSpecific(ListView.java:1356)
at android.widget.ListView.layoutChildren(ListView.java:1651)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-15 22:03:57.380 2651-2651/? I/Process﹕ Sending signal. PID: 2651 SIG: 9