ExpandableListView throwing InflateException when setting an adapter
My app is throwing a android.view.InflateException when I try to set an
adapter to a ListView. The code runs fine doing the last line of onCreate
where I call setAdapter(...) but then it fails in the next line at the end
of onCreate. The code runs without errors when I do comment out the
setAdapter method.
Here are some of the logs.
09-15 17:38:20.986: W/dalvikvm(26497): threadid=1: thread exiting with
uncaught exception (group=0x40cf8438)
09-15 17:38:21.136: E/AndroidRuntime(26497): FATAL EXCEPTION: main
09-15 17:38:21.136: E/AndroidRuntime(26497):
android.view.InflateException: Binary XML file line #10: Error inflating
class <unknown>
09-15 17:38:21.136: E/AndroidRuntime(26497): at
android.view.LayoutInflater.createView(LayoutInflater.java:613)
09-15 17:38:21.136: E/AndroidRuntime(26497): at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-15 17:38:21.136: E/AndroidRuntime(26497): at
android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
09-15 17:38:21.136: E/AndroidRuntime(26497): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
FavoritesActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
setTitle("Favorites");
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,
listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}//END OF ONCREATE
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
//Get current data
SharedPreferences pref =
getApplicationContext().getSharedPreferences("MyPref", PRIVATE_MODE);
Set<String> listDataHeaderSet = pref.getStringSet(LIST_DATA_HEADER,
null);
if(!(listDataHeaderSet == null)){
listDataHeader.addAll(listDataHeaderSet);
}
for(int i = 0; i < listDataHeader.size(); i++){
listDataChild.put(listDataHeader.get(i), addChildren());
}
}
/**
* Returns a list of the children in the listview
* @return
*/
private List<String> addChildren(){
List<String> locationChildren = new ArrayList<String>();
locationChildren.add("set perm start");
locationChildren.add("set start");
locationChildren.add("set perm end");
locationChildren.add("set end");
return locationChildren;
}
ExpandableListAdapter.java
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, 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.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 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) {
String headerTitle = (String) 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);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
activity_favorites.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="30sp"
android:textColor="@android:style/Holo.ButtonBar" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
/>
</LinearLayout>
No comments:
Post a Comment