我想以基本格式编写ListView
但我收到错误:
UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
和
androidview.LayoutInfalater.inflate(LayoutInflater.java: some numbers....like 720,658...so on)
我知道应该在适配器类中完成一些事情:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
RelativeLayout rv = new RelativeLayout(c);
TextView tv = new TextView(c);
TextView tv1 = new TextView(c);
ImageView imgv = new ImageView(c);
tv.setText(s[position]);
tv1.setText(i[position]);
imgv.setImageResource(d[position]);
rv.addView(tv);
rv.addView(tv1);
rv.addView(imgv);
return rv;
}
我该怎么做才能解决问题
LOGCAT:
02-20 16:40:24.967: E/Trace(1715): error opening trace file: No such file or directory (2)
02-20 16:40:25.819: W/ResourceType(1715): No package identifier when getting value for resource number 0x000020d0
02-20 16:40:25.819: D/AndroidRuntime(1715): Shutting down VM
02-20 16:40:25.819: W/dalvikvm(1715): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-20 16:40:25.857: E/AndroidRuntime(1715): FATAL EXCEPTION: main
02-20 16:40:25.857: E/AndroidRuntime(1715): android.content.res.Resources$NotFoundException: String resource ID #0x20d0
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.content.res.Resources.getText(Resources.java:229)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.TextView.setText(TextView.java:3620)
02-20 16:40:25.857: E/AndroidRuntime(1715): at com.example.systemzap2.adapt.getView(adapt.java:59)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.AbsListView.obtainView(AbsListView.java:2271)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.ListView.onMeasure(ListView.java:1156)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.View.measure(View.java:15172)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.View.measure(View.java:15172)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.View.measure(View.java:15172)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.View.measure(View.java:15172)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 16:40:25.857: E/AndroidRuntime(1715): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.View.measure(View.java:15172)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.Choreographer.doFrame(Choreographer.java:525)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.os.Handler.handleCallback(Handler.java:615)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.os.Looper.loop(Looper.java:137)
02-20 16:40:25.857: E/AndroidRuntime(1715): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-20 16:40:25.857: E/AndroidRuntime(1715): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 16:40:25.857: E/AndroidRuntime(1715): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 16:40:25.857: E/AndroidRuntime(1715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-20 16:40:25.857: E/AndroidRuntime(1715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-20 16:40:25.857: E/AndroidRuntime(1715): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:111)
我该怎么办?
更正您的代码。
UnsupportedOperationException:addView(View,LayoutParams)不是 AdapterView支持
像AdapterView
这样的ListView
的子类不能在布局文件中手动添加子项或在代码中添加子项。所以,如果你在一个布局中有这个:
<ListView // .. other attributes>
<// other views <-- notice the children of the ListView tag
</ListView>
不要这样做,因为这会调用addView
的{{1}}方法,抛出异常。而是使用:
ListView
你也 在代码中使用<ListView // .. other attributes />
< // other views
addView
方法中的任何一种方法:
ListView
此外,如果您在listViewReference.addView(anotherView); // <-- don't do it
或适配器(其LayoutInflater.inflate
方法)的代码中使用Activity
方法,则不会传递 getView
作为第二个参数。例如,不使用:
ListView
像在Tamilarasi Sivaraj的回答一样,这将再次引发异常。而是使用:
convertView = inflator.inflate(R.layout.child_rows, parent);
与您在问题中发布的异常相关,您似乎使用convertView = inflator.inflate(R.layout.child_rows, parent, false);
方法setText
(其中一个int
或s
数组为{ {1}}数组)。问题是,在这种情况下,i
会认为您正在尝试使用像int
这样的字符串资源来设置文本。您传递的TextView
不是字符串资源,因此会抛出异常。如果R.string.astring
或int
是s
而您试图在i
中显示,请改用:
int
答案 1 :(得分:6)
替换inflater中的布局
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) LayoutInflater
.from(contex);
convertView = inflater.inflate(R.layout.your_layout, parent, false);
}
txtName = (TextView) convertView.findViewById(R.id.txtName);
txtName.setText(""+ContactsArr.get(position).get("ContName"));
txtPhoneNumber = (TextView) convertView.findViewById(R.id.txtContact);
txtPhoneNumber.setText(""+ContactsArr.get(position).get("ContPhone"));
return convertView;
}
答案 2 :(得分:5)
如果您喜欢我,并完全跳过我在@Lacksprog的答案中所需要的答案,因为它已经埋没在其中,请尝试将第三个参数添加到inflate
方法,也提到here:
inflater.inflate(R.layout.group_row, parent, false);
请勿使用true
代替false
,否则您会收到相同的错误,因为它会尝试将其附加/添加到父级,因此addView is not supported in AdapterView
错误。
答案 3 :(得分:1)
试试这样:
public static class ViewHolder {
public ImageView imageView;
public TextView textView;
public View getView(int position, View convertView, ViewGroup parent) {
try{
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = context1.getLayoutInflater();
convertView = inflater.inflate(R.layout.your_layout, null,true);
holder.imageView = (ImageView) convertView.findViewById(R.id.imgview);
holder.textView = (TextView) convertView.findViewById(R.id.txtview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText("your text");
holder.imageView.setImageResource("your image resource");
} catch (Exception e) {
System.out.println("Error: "+e);
}
return convertView;
}
}
答案 4 :(得分:0)
我也收到了这个问题:
<块引用>android.view.InflateException:二进制 XML 文件第 2 行:addView(View, LayoutParams) 在 AdapterView 中不受支持,原因是: java.lang.UnsupportedOperationException: addView(View, LayoutParams) AdapterView 不支持
我在给 .xml
充气时忘记添加 "false"。添加后它就可以工作了。我的代码短语如下。我为我的 LinearLayout
使用了 .xml
。
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_format,parent,false);
}