无法启动的活动:UnsupportedOperationException异常:addView(查看,的LayoutParams)不支持适配器视图 [英] Unable to start activity:UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
问题描述
我想在基本格式编写的ListView
,但我得到一个错误:
UnsupportedOperationException异常:addView(查看,的LayoutParams)不支持适配器视图
和
androidview.LayoutInfalater.inflate(LayoutInflater.java:一些数字....像720658 ...等等)
我知道的东西应该在适配器类在这里完成的:
公开查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
RelativeLayout的RV =新RelativeLayout的(C);
TextView的电视=新的TextView(C);
TextView的TV1 =新的TextView(C);
ImageView的imgv =新ImageView的(C);
tv.setText(S [位置]);
tv1.setText(我[位置]);
imgv.setImageResource(D [位置]);
rv.addView(电视);
rv.addView(TV1);
rv.addView(imgv);
返回rv;
}
我应该怎么做才能解决这个问题。
在logcat的:
02-20 16:40:24.967:E /跟踪(1715):错误打开跟踪文件:没有这样的文件或目录(2)
02-20 16:40:25.819:W /的ResourceType(1715):无包装标识中获得价值的资源数量0x000020d0时
02-20 16:40:25.819:D / AndroidRuntime(1715):关闭虚拟机
02-20 16:40:25.819:W / dalvikvm(1715):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a13300)
02-20 16:40:25.857:E / AndroidRuntime(1715):致命异常:主要
02-20 16:40:25.857:E / AndroidRuntime(1715):android.content.res.Resources $ NotFoundException:字符串资源ID#0x20d0
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.content.res.Resources.getText(Resources.java:229)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.TextView.setText(TextView.java:3620)
02-20 16:40:25.857:E / AndroidRuntime(1715):在com.example.systemzap2.adapt.getView(adapt.java:59)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.AbsListView.obtainView(AbsListView.java:2271)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.ListView.onMeasure(ListView.java:1156)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.View.measure(View.java:15172)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.View.measure(View.java:15172)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.View.measure(View.java:15172)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.View.measure(View.java:15172)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-20 16:40:25.857:E / AndroidRuntime(1715):在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2148)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.View.measure(View.java:15172)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4214)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.Choreographer.doFrame(Choreographer.java:525)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.os.Handler.handleCallback(Handler.java:615)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.os.Handler.dispatchMessage(Handler.java:92)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.os.Looper.loop(Looper.java:137)
02-20 16:40:25.857:E / AndroidRuntime(1715):在android.app.ActivityThread.main(ActivityThread.java:4745)
02-20 16:40:25.857:E / AndroidRuntime(1715):在java.lang.reflect.Method.invokeNative(本机方法)
02-20 16:40:25.857:E / AndroidRuntime(1715):在java.lang.reflect.Method.invoke(Method.java:511)
02-20 16:40:25.857:E / AndroidRuntime(1715):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
02-20 16:40:25.857:E / AndroidRuntime(1715):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-20 16:40:25.857:E / AndroidRuntime(1715):在dalvik.system.NativeStart.main(本机方法)
我应该怎么办???
纠正你的code。
UnsupportedOperationException异常:addView(查看,的LayoutParams)不 在适配器视图支持
的适配器视图
A子像的ListView
不能有孩子手动添加无论是在布局文件或添加在code。所以,如果你有这样的在你的布局之一:
< ListView控件// ..其他属性>
< //其他意见< - 注意的ListView标签的孩子
< / ListView控件>
不这样做,,因为这会调用的ListView的
,抛出异常。取而代之的是: addView
方法
< ListView控件// ..其他属性/>
< //其他意见
您还不能使用任何的的ListView
中的 addView
方法code是这样的:
listViewReference.addView(anotherView); //< - 不这样做
另外,如果你在活动的code使用
或适配器(其 LayoutInflater.inflate
方法 getView
法),不及格的的ListView
作为第二个参数。例如,不要使用
convertView = inflator.inflate(R.layout.child_rows,父);
在Tamilarasi Sivaraj的答案,因为这会再次抛出异常。取而代之的是:
convertView = inflator.inflate(R.layout.child_rows,父母,假);
与你张贴在讨论的异常,看来你使用的setText
方法与 INT
(的■一个
或我
阵列作为一个 INT
阵列)。问题是,在这种情况下的TextView
会认为你想使用这样的字符串资源 R.string.astring 设置文本code>。该
INT
您传递是不是一个字符串资源使一个异常将被抛出。如果取值
或我
是 INT
和你试图证明它在的TextView
用这个来代替:
tv.setText(将String.valueOf(S [位置])); //假设s是int数组
I want to write a ListView
in basic format but I get an error:
UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
and:
androidview.LayoutInfalater.inflate(LayoutInflater.java: some numbers....like 720,658...so on)
I know something should be done here in the adapter class:
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;
}
What should I do to solve the problems
The 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)
what should i do???
Correct your code.
UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
A subclass of AdapterView
like a ListView
can't have children manually added either in the layout file or added in code. So if you have this in one of your layouts:
<ListView // .. other attributes>
<// other views <-- notice the children of the ListView tag
</ListView>
don't do it, as this will call the addView
method of ListView
, throwing the exception. Instead use:
<ListView // .. other attributes />
< // other views
You also can't use any of the addView
methods of ListView
in code like this:
listViewReference.addView(anotherView); // <-- don't do it
Also, if you use the LayoutInflater.inflate
method in the code of the Activity
or the adapter(its getView
method), don't pass the ListView
as the second parameter. For example, don't use:
convertView = inflator.inflate(R.layout.child_rows, parent);
as in Tamilarasi Sivaraj's answer as that will throw the exception again. Instead use:
convertView = inflator.inflate(R.layout.child_rows, parent, false);
Related to the exception you posted in the question, it appears you use the setText
method with an int
(one of the s
or i
arrays being an int
array). The problem is that in this case TextView
will think you're trying to set the text using a string resource like this R.string.astring
. The int
you pass is not a string resource so an exception will be thrown. If s
or i
is an int
and you're trying to show it in the TextView
use this instead:
tv.setText(String.valueOf(s[position])); // assuming s is the int array
这篇关于无法启动的活动:UnsupportedOperationException异常:addView(查看,的LayoutParams)不支持适配器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!