gpt4 book ai didi

android - 带有组和子复选框的可扩展 ListView 给出空指针异常

转载 作者:太空狗 更新时间:2023-10-29 15:01:44 25 4
gpt4 key购买 nike

I have an expandable listview with checkboxes in group and child. 

我想要的是当我选中组复选框时,它的所有子项都应该被选中。但是当我运行程序时,我得到了空指针异常。这是我的代码......谢谢

//ExpandableListAdapter.java:

import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.view.View.OnClickListener;


public class ExpandableListAdapter extends BaseExpandableListAdapter {

LayoutInflater inflater;

/*list of group */
private ArrayList<Group> groups;
private ArrayList<Boolean> isCheckedHeader=new ArrayList<Boolean>();
private HashMap<String, ArrayList<Boolean>> IsCheckedChild;


public ExpandableListAdapter(Context context,ArrayList<Group> groups,ArrayList<Boolean> isCheckedHeader,HashMap<String, ArrayList<Boolean>> IsCheckedChild) {
super();
this.groups=groups;
this.isCheckedHeader=isCheckedHeader;
this.IsCheckedChild=IsCheckedChild;
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* @param child
* @param group
* use for adding item to list view
*/
public void addItem(Child child,Group group) {
if(!groups.contains(group)) {
groups.add(group);
}
int index=groups.indexOf(group);
ArrayList<Child> ch=groups.get(index).getChildrens();
ch.add(child);
groups.get(index).setChildrens(ch);
}

public Child getChild(int groupPosition, int childPosition) {
ArrayList<Child> ch=groups.get(groupPosition).getChildrens();

return ch.get(childPosition);
}

public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> ch=groups.get(groupPosition).getChildrens();
return ch.size();
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Child child= (Child) getChild(groupPosition,childPosition);
TextView childName=null;
if(convertView==null) {
convertView=inflater.inflate(R.layout.child_view, null);
}

final CheckBox chkChild=(CheckBox)convertView.findViewById(R.id.checkBoxC);
childName=(TextView) convertView.findViewById(R.id.textViewChildName);
childName.setText(child.getChildName());
chkChild.setChecked(IsCheckedChild.get(this.groups.get(groupPosition)).get(childPosition));

final int iGroupPos = groupPosition;
final int iChildPos = childPosition;

chkChild.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkChild.isChecked()) //the child was enable (checked)
{
IsCheckedChild.get(groups.get(iGroupPos)).set(iChildPos, true);
}
else //the child was disable (non-checked)
{
IsCheckedChild.get(groups.get(iGroupPos)).set(iChildPos, false);
}
}
});

return convertView;
}
public Group getGroup(int groupPosition) {
return groups.get(groupPosition);
}

public int getGroupCount() {
return groups.size();
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView groupName = null;
Group group=(Group) getGroup(groupPosition);
if(convertView==null) {
convertView=inflater.inflate(R.layout.group_view, null);
}

groupName=(TextView) convertView.findViewById(R.id.textViewGroupName);
groupName.setText(group.getGroupName());
final CheckBox chkParent=(CheckBox)convertView.findViewById(R.id.checkBoxH);
chkParent.setChecked(isCheckedHeader.get(groupPosition));
final int iPos = groupPosition;
chkParent.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkParent.isChecked()) {
isCheckedHeader.set(iPos, true);
//Checking all SubItem(Childs) of this
int iSizeChilds = getChildrenCount(iPos);
for(int i=0;i<iSizeChilds;i++) {
IsCheckedChild.get(groups.get(iPos)).set(i, true);
}
}
notifyDataSetChanged();
}
});
return convertView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

public boolean hasStableIds() {
return true;
}
}

//组.java:

import java.util.ArrayList;

public class Group {
public String groupId;
public String groupName;
public ArrayList<Child> childrens;
public Group(String groupId, String groupName,
ArrayList<Child> childrens) {
super();
this.groupId = groupId;
this.groupName = groupName;
this.childrens = childrens;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public ArrayList<Child> getChildrens() {
return childrens;
}
public void setChildrens(ArrayList<Child> childrens) {
this.childrens = childrens;
}

}

//Child.java:

public class Child {
public String chiledId;
public String childName;
public Child(String chiledId, String childName) {
super();
this.chiledId = chiledId;
this.childName = childName;
}
public String getChiledId() {
return chiledId;
}
public void setChiledId(String chiledId) {
this.chiledId = chiledId;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
}

//在MainActivity中动态添加项目。

    MainActivity.java:

public class MainActivity extends Activity {
/*our expandable adapter */
ExpandableListAdapter expandableListAdapter;
/*expandable list*/
ExpandableListView expandableListView;
/*list items*/
ArrayList<Group> groups=new ArrayList<Group>();
ArrayList<Boolean> isCheckedHeader;
HashMap<String, ArrayList<Boolean>> IsCheckedChild;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isCheckedHeader=new ArrayList<Boolean>();
IsCheckedChild=new HashMap<String, ArrayList<Boolean>>();
/*genarate data for list view*/
genarateData();
/*instantiate adapter with our item list*/
expandableListAdapter=new ExpandableListAdapter(this, groups,isCheckedHeader,IsCheckedChild);
/*we get list view*/
expandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
/*set adapter to list view*/
expandableListView.setAdapter(expandableListAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//Generate dummy data for list view
public void genarateData() {
Group group;

for (int i = 0; i < 10; i++) {

ArrayList<Child> childrens=new ArrayList<Child>();
childrens.clear();
Child child;

for (int j = 0; j < 5; j++) {
child=new Child(""+j, "Child "+j);
childrens.add(child);
}
group=new Group(""+i, " Group "+i, childrens);
groups.add(group);
isCheckedHeader.add(false);
}
}
}

这是我的日志。

09-16 03:24:19.341: I/Process(1863): Sending signal. PID: 1863 SIG: 9
09-16 03:24:27.871: D/dalvikvm(1903): GC_FOR_ALLOC freed 59K, 5% free 2949K/3092K, paused 31ms, total 32ms
09-16 03:24:27.871: I/dalvikvm-heap(1903): Grow heap (frag case) to 3.562MB for 635812-byte allocation
09-16 03:24:27.921: D/dalvikvm(1903): GC_FOR_ALLOC freed 2K, 4% free 3567K/3716K, paused 40ms, total 40ms
09-16 03:24:28.441: D/AndroidRuntime(1903): Shutting down VM
09-16 03:24:28.441: W/dalvikvm(1903): threadid=1: thread exiting with uncaught exception (group=0xb3a12ba8)
09-16 03:24:28.451: E/AndroidRuntime(1903): FATAL EXCEPTION: main
09-16 03:24:28.451: E/AndroidRuntime(1903): Process: com.android.guide.expandablelistview, PID: 1903
09-16 03:24:28.451: E/AndroidRuntime(1903): java.lang.NullPointerException
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.guide.expandablelistview.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:117)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.makeAndAddView(ListView.java:1790)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.fillDown(ListView.java:691)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.fillFromTop(ListView.java:752)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.layoutChildren(ListView.java:1630)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer.doFrame(Choreographer.java:544)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.os.Handler.handleCallback(Handler.java:733)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.os.Looper.loop(Looper.java:136)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-16 03:24:28.451: E/AndroidRuntime(1903): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 03:24:28.451: E/AndroidRuntime(1903): at java.lang.reflect.Method.invoke(Method.java:515)
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-16 03:24:28.451: E/AndroidRuntime(1903): at dalvik.system.NativeStart.main(Native Method)
09-16 03:25:11.961: I/Process(1903): Sending signal. PID: 1903 SIG: 9


How can I correct this?
PS:I'm a beginner. Thanks :)

最佳答案

试试这段代码,

使用 final Parent parent = parents.get(groupPosition); 而不是使用

Group group=(Group) getGroup(groupPosition);

然后更改checkbox setcheck-

CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
checkbox.setChecked(parent.isChecked());

查看此链接:link

关于android - 带有组和子复选框的可扩展 ListView 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com