这是在getView()中通货膨胀时的CountryDrawerListAdapter我得到java.lang.RuntimeException:二进制XML文件行#10:你必须提供layout_width属性。
public class CountryDrawerListAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private LayoutInflater mLayoutInflater;
private Viewholder mHolder;
private InputStream mInputStream = null;
private ItemFilter mFilter = new ItemFilter();
private List<String> mFilteredData = null;
private ArrayList<String> mData;
public CountryDrawerListAdapter(Context context, ArrayList<String> data, ListView list) {
this.mContext =context;
this.mFilteredData =data;
this.mData =data;
mLayoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private static class Viewholder {
TextView text;
ImageView icon;
CheckBox ch_box;
}
@Override
public int getCount() {
if (mFilteredData ==null) {
return 0;
}
else return mFilteredData.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if (convertView == null) {
vi = mLayoutInflater.inflate(R.layout.a_clientslist_layout, null);
mHolder = new Viewholder();
mHolder.text = (TextView) vi.findViewById(R.id.tv_client_name);
mHolder.icon = (ImageView) vi.findViewById(R.id.img_client_logo);
vi.setTag(mHolder);
}
else {
mHolder = (Viewholder) vi.getTag();
}
mHolder.text.setText(mFilteredData.get(position));
Drawable d;
try {
String flags=null;
/* "images/client_1.png"*/
if(mFilteredData.get(position).length()>0)
{
flags= mFilteredData.get(position).replaceAll(" ", "_");
}
d = Drawable.createFromStream(mContext.getAssets().open("flags/"+flags+".png"), null);
if (d==null){
mHolder.icon.setImageResource(R.drawable.ic_launcher);
}
mHolder.icon.setBackground(d);
} catch (IOException e) {
LogCat.e("IOException", "" + e.toString());
}
return vi;
}
public Filter getFilter() { return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = mData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
这是将由适配器充气的Xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/img_client_logo"
android:layout_width="@dimen/drawer_img_size_width"
android:layout_height="@dimen/drawer_img_size_hight"
android:layout_marginLeft="@dimen/listvie_left_padding"
/>
<TextView
android:id="@+id/tv_client_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:paddingLeft="20dp"
android:textSize="@dimen/drawer_textsize"
android:text=""
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/white" />
</LinearLayout>
这是我的日志Cat显示错误。
02-16 10:40:10.183 9565-9565/com.android.xyz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android.xyz , PID: 9565
**java.lang.RuntimeException: Binary XML file line #10: You must supply a layout_width attribute.**
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:492)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5948)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6117)
at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1831)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1743)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:58)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:757)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.xyz.adapter.CountryDrawerListAdapter.getView(CountryDrawerListAdapter.java:69)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:866)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(H
答案 0 :(得分:0)
尝试两件事