Java 膨胀类TextView时出现可扩展Listview错误
我在我的主要Java 膨胀类TextView时出现可扩展Listview错误,java,android,android-layout,listview,expandablelistview,Java,Android,Android Layout,Listview,Expandablelistview,我在我的主要活动中使用了材料设计tablayout,在那里我有可滚动的选项卡,在我的第一个片段中我使用了可扩展的列表视图 这是我的片段代码 public class House_Hold_List_Fragment extends Fragment { ExpandableListAdapter listAdapter; ExpandableListView expListView; List<HouseHoldList_Model> listDataHead
活动中使用了材料设计tablayout
,在那里我有可滚动的选项卡,在我的第一个片段中我使用了可扩展的列表视图
这是我的片段代码
public class House_Hold_List_Fragment extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<HouseHoldList_Model> listDataHeader;
HashMap<HouseHoldList_Model, List<String>> listDataChild;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.house_hold_list_fragment, container, false);
setHasOptionsMenu(true);
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
listDataHeader = new ArrayList<HouseHoldList_Model>();
listDataChild = new HashMap<HouseHoldList_Model, List<String>>();
loadModel();
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
return view;
}
public void prepareListData(){
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put((HouseHoldList_Model)listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
public void loadModel(){
listDataHeader.add(new HouseHoldList_Model("A"));
listDataHeader.add(new HouseHoldList_Model("B"));
listDataHeader.add(new HouseHoldList_Model("C"));
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
//super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
//do something
Log.e("search clicked","sd");
return true;
case R.id.action_settings:
//do something
Log.e("search clicked","sd");
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的ExpandableListAdapter类代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<HouseHoldList_Model> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<HouseHoldList_Model
, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<HouseHoldList_Model> listDataHeader,
HashMap<HouseHoldList_Model, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.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.house_holld_list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.house_hold_list_item);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object 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) {
HouseHoldList_Model model = (HouseHoldList_Model) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
lblListHeader.setText(model.name);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的住店名单排
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_centerInParent="true"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/house_hold_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:padding="8dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textColor="@color/white"
android:background="@drawable/hh_count_round"
android:layout_weight=".5"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_weight="2.2"
android:orientation="vertical">
<TextView
android:id="@+id/house_hold_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="@color/blacktext"
android:text="Muhammad Ramza"/>
<TextView
android:id="@+id/house_hold_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main double road g-11"
android:textColor="@color/blacktext"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family Members"
android:textColor="@color/blacktext" />
<TextView
android:id="@+id/house_hold_fm_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:layout_marginLeft="10dp"
android:background="@drawable/hh_fm_count_round"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/down"
android:layout_marginRight="5dp"
android:layout_gravity="center"
android:layout_weight=".3"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
在我看来,您的textview无法引用convertView,因此请在if()条件下使用它,然后尝试下面的代码
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null)
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
lblListHeader.setText(model.name);
}
张贴右布局图。房屋保留清单_row@ADM我补充说,错误日志清楚地表明,行xml文件的textview id中没有匹配项。请先自行检查。此xml中不存在house\u hold\u list\u行,可能是因为复制粘贴。使其正确文本视图的id我检查了@ADM,但有相同的错误为什么会这样?它如何修复在inflate()
调用中抛出的InflateException
?这甚至不会编译,因为lblListHeader
超出了if
块的范围。可能是您的条件没有按您想要的那样工作,所以首先检查“if”条件是否正常工作,更好的方法是尝试删除“if”条件……我检查了OMI
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_centerInParent="true"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/house_hold_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:padding="8dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textColor="@color/white"
android:background="@drawable/hh_count_round"
android:layout_weight=".5"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_weight="2.2"
android:orientation="vertical">
<TextView
android:id="@+id/house_hold_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="@color/blacktext"
android:text="Muhammad Ramza"/>
<TextView
android:id="@+id/house_hold_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main double road g-11"
android:textColor="@color/blacktext"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family Members"
android:textColor="@color/blacktext" />
<TextView
android:id="@+id/house_hold_fm_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:layout_marginLeft="10dp"
android:background="@drawable/hh_fm_count_round"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/down"
android:layout_marginRight="5dp"
android:layout_gravity="center"
android:layout_weight=".3"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null)
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
lblListHeader.setText(model.name);
}
This will work
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
HouseHoldList_Model model = (HouseHoldList_Model) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
}
lblListHeader.setText(model.name);
return convertView;
}