我有一个listView的自定义适配器。当我尝试给某些View充气时,会抛出一个异常。这是我的getView()
代码:
public View getView(int position, View convertView, ViewGroup parent){
View row = mInflater.inflate(R.layout.items_list_element, parent);
CheckBox cb = (CheckBox)row.findViewById(R.id.gotItCheckBox);
String checked = String.valueOf(cb.isChecked());
return row;
}
行View row = mInflater.inflate(R.layout.items_list_element,parent);抛出这个:
04-16 10:37:59.135 625-625/com.grizeldi.gShopper E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:461)
at android.view.LayoutInflater.inflate(LayoutInflater.java:416)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.grizeldi.gShopper.adapter.getView(adapter.java:21)
at android.widget.AbsListView.obtainView(AbsListView.java:1430)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
at android.widget.ListView.onMeasure(ListView.java:1127)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
任何人都知道发生了什么?
编辑:我确保mInflater不为空。答案 0 :(得分:1)
更改
View row = mInflater.inflate(R.layout.items_list_element, parent);
与
View row = mInflater.inflate(R.layout.items_list_element, parent, false);
答案 1 :(得分:0)
尝试以下代码: -
View row=null;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
}