我尝试在可扩展列表视图中传递我的自定义数组适配器,但是我遇到了问题空点异常。在组视图中,我传递自定义arrayadapter而不是字符串,但子视图仍然是字符串。这工作正常,直到我将String转换为我的自定义Arrayadapter并在那里放置多个视图。有一个警告显示在getchild方法中对hashmap的可疑调用,我认为这是问题所在。请帮忙
这是我的logcat
06-02 03:08:35.971 7966-7966/me.ashishgm.expandablemenu E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.ashishgm.expandablemenu, PID: 7966
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at me.ashishgm.expandablemenu.ExpandableListAdapter.getChild(ExpandableListAdapter.java:31)
at me.ashishgm.expandablemenu.ExpandableListAdapter.getChildView(ExpandableListAdapter.java:42)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillSpecific(ListView.java:1412)
at android.widget.ListView.layoutChildren(ListView.java:1708)
at android.widget.AbsListView.onLayout(AbsListView.java:2162)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
以下是我的可扩展适配器的代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<people_detail> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<people_detail, List<String>> _listDataChild;
ExpandableListAdapter(Context context, List<people_detail> listDataHeader,
HashMap<people_detail, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
//I think i have problem in this part and this is also showing the warning Suspicious call to HashMap
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataChild.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 people_detail 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) {
people_detail headerTitle = (people_detail) 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);
TextView checktest=(TextView) convertView.findViewById(R.id.lblListHeader2);
TextView checkok=(TextView) convertView.findViewById(R.id.lblListHeader3);
ImageView img_vr=(ImageView)convertView.findViewById(R.id.image_view);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getPatientName());
checktest.setText(headerTitle.gethospitalName());
checkok.setText(headerTitle.gettimeRequired());
switch (groupPosition)
{
case 0:
checktest.setText("Nepal");
img_vr.setImageResource(R.drawable.ic_devices_other_black_24dp);
break;
case 1:
checktest.setText("India");
img_vr.setImageResource(R.drawable.ic_phonelink_setup_black_24dp);
break;
case 2:
checktest.setText("China");
break;
default:
checktest.setText("Africa");
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}}
我的mainActivity在这里
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<people_detail> listDataHeader;
HashMap<people_detail, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<people_detail>();
listDataChild = new HashMap<people_detail,List<String>>();
// Adding child data
listDataHeader.add(new people_detail("Top 250","Nepal","Fuck",R.drawable.ic_assignment_returned_black_24dp));
listDataHeader.add(new people_detail("Top 250","Nepal","Fuck in the sun",R.drawable.ic_assignment_returned_black_24dp));
listDataHeader.add(new people_detail("Navana timro","Nepal","Fuck in the sun",R.drawable.ic_assignment_returned_black_24dp));
listDataHeader.add(new people_detail("hello hello","Nepal","Fuck in the sun",R.drawable.ic_assignment_returned_black_24dp));
listDataHeader.add(new people_detail("Yo Jindagi ho","Nepal","Fuck in the sun",R.drawable.ic_assignment_returned_black_24dp));
listDataHeader.add(new people_detail("Top 250","Nepal","Khai k vanne ",R.drawable.ic_assignment_returned_black_24dp));
listDataHeader.add(new people_detail("you are beast","Nepal","k navanne",R.drawable.ic_assignment_returned_black_24dp));
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
我的人员详细课程在这里
public class people_detail {
// Name of the Android version (e.g. Gingerbread, Honeycomb, Ice Cream Sandwich)
private String mpatientName;
// Android version number (e.g. 2.3-2.7, 3.0-3.2.6, 4.0-4.0.4)
private String mhospitalName;
private String timeRequired;
// Drawable resource ID
private int mImageResourceId;
/*
* Create a new AndroidFlavor object.
*
* @param vName is the name of the Android version (e.g. Gingerbread)
* @param vNumber is the corresponding Android version number (e.g. 2.3-2.7)
* @param image is drawable reference ID that corresponds to the Android version
* */
public people_detail(String vpatientName, String vhospitalName, String vtimeRequired , int imageResourceId)
{
mpatientName=vpatientName;
mhospitalName=vhospitalName;
timeRequired=vtimeRequired;
mImageResourceId = imageResourceId;
}
/**
* Get the name of the Android version
*/
public String getPatientName() {
return mpatientName ;
}
/**
* Get the Android version number
*/
public String gethospitalName() {
return mhospitalName;
}
/**
* Get the hospital Name
*/
public String gettimeRequired() {
return timeRequired;
}
/**
* Get the image resource ID
*/
public int getImageResourceId() {
return mImageResourceId;
}
}
答案 0 :(得分:0)
它是this._listDataChild.get(groupPosition)
处的空指针异常,您声明groupPosition是int但从未给它赋值。