我对基于Fragment的活动很陌生。我正在使用CollapsingToolbarLayout中的可折叠列表视图。
这里,我在java文件中面临无法访问的语句错误。无法摆脱它。
我的代码 -
public class MenuActivity extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_booktable, container, false);
// get the listview
expListView = (ExpandableListView) container.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("South Indian");
listDataHeader.add("Quick Bites");
listDataHeader.add("Soups");
// Adding child data
List<String> SouthIndian = new ArrayList<String>();
SouthIndian.add("Kara Bath");
SouthIndian.add("Chow Chow Bath");
SouthIndian.add("2 Idli 1 Vada");
SouthIndian.add("Rava Idli");
SouthIndian.add("Curd Vada");
SouthIndian.add("Masala Dosa");
List<String> QuickBites = new ArrayList<String>();
QuickBites.add("Veg Sandwich");
QuickBites.add("Veg Toast Sandwich");
QuickBites.add("Bread Butter Jam");
QuickBites.add("Bread Jam");
QuickBites.add("Pakoda");
QuickBites.add("Masala Puri");
List<String> Soups = new ArrayList<String>();
Soups.add("Tomato Soup");
Soups.add("Sweet corn Soup");
Soups.add("Veg Soup");
Soups.add("Veg Schezwan Soup");
Soups.add("Veg Noodles Soup");
listDataChild.put(listDataHeader.get(0), SouthIndian); // Header, Child data
listDataChild.put(listDataHeader.get(1), QuickBites);
listDataChild.put(listDataHeader.get(2), Soups);
}
}
如何解决行 expListView =(ExpandableListView)container.findViewById(R.id.lvExp); 的错误并使此代码运行?
答案 0 :(得分:0)
onCreateView的第一行已返回View,因此其余代码未执行:
return inflater.inflate(R.layout.activity_booktable, container, false);
您必须将其替换为
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.activity_booktable, container, false);
...
return myView;
}
然后在这个视图上更好地执行findViewById,例如
expListView = (ExpandableListView) myView.findViewById(R.id.lvExp);
答案 1 :(得分:0)
将return inflater.inflate(R.layout.activity_booktable, container, false)
移至方法onCreateView
()