Unable to start activity:UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
Solution 1
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
Solution 2
Replace your layout in 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;
}
Solution 3
If you're like me and totally skipped over the answer I needed in @Luksprog's answer because it's buried in it, try using adding the third parameter to the inflate
method, also mentioned here:
inflater.inflate(R.layout.group_row, parent, false);
Don't use true
instead of false
or you will get the same error that brought you here, because it's trying to attach/add it to the parent, hence the addView is not supported in AdapterView
error.
Solution 4
Try this Like this:
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;
}
}
Comments
-
elegance almost 2 years
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)
-
elegance over 11 yearsTamilarasi Sivaraj, should i put my tex t view and img view in the place of the child view???
-
Android_coder over 11 yearsyes you put your textview and image view in the child view and mention the layout name in LayoutInflater...if you have any queries ask me
-
Android_coder over 11 yearsi have edited my code for put textview and imageview from layout to listview using adapter use it...
-
Mushtaq Jameel almost 10 years+1 for the "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"
-
Thom over 7 yearsLooks like this applies to GridView as well. :(
-
AmiNadimi almost 5 yearsThe part that was not directly answering the question, solved my problem! (inflation)
-
Heinzlmaen over 4 yearsSo what's the correct way to do this if addView() doesn't work in code?
-
Matthew Cawley about 4 yearsThank you for the:
convertView = inflator.inflate(R.layout.child_rows, parent, false);