41

我想以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)
4

5 回答 5

126

我该怎么办???

更正您的代码。

UnsupportedOperationException:AdapterView 不支持 addView(View, LayoutParams)

AdapterView像 a 这样的子类ListView 不能在布局文件中手动添加子类,也不能在代码中添加子类。因此,如果您在其中一种布局中有此内容:

<ListView // .. other attributes>
     <// other views <-- notice the children of the ListView tag
</ListView>

不要这样做,因为这会调用的addView方法ListView,抛出异常。而是使用:

<ListView // .. other attributes />
< // other views

您也不能使用以下代码中的任何addView方法ListView

listViewReference.addView(anotherView); // <-- don't do it

此外,如果您在或适配器LayoutInflater.inflate的代码中使用该方法(其方法),请不要将其作为第二个参数传递。例如,不要使用ActivitygetViewListView

convertView  = inflator.inflate(R.layout.child_rows, parent);

就像在 Tamilarasi Sivaraj 的回答中那样,这将再次引发异常。而是使用:

convertView  = inflator.inflate(R.layout.child_rows, parent, false);

与您在问题中发布的异常相关,您似乎将该setText方法与一个intsi数组之一是int数组)一起使用。问题是,在这种情况下,TextView您会认为您正在尝试使用这样的字符串资源来设置文本R.string.astring。您传递的int不是字符串资源,因此将引发异常。如果sori是 anint并且您试图在TextViewuse this 中显示它:

tv.setText(String.valueOf(s[position])); // assuming s is the int array
于 2013-02-20T11:40:47.500 回答
7

在充气机中替换您的布局

 @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;
}
于 2013-02-20T11:13:49.783 回答
5

如果您像我一样完全跳过了@Luksprog 的答案中我需要的答案,因为它被埋在其中,请尝试使用将第三个参数添加到inflate方法中,这里也提到

inflater.inflate(R.layout.group_row, parent, false);

不要使用true代替,false否则您将得到与将您带到这里的相同错误,因为它试图将其附加/添加到父级,因此出现addView is not supported in AdapterView错误。

于 2016-01-06T17:51:30.177 回答
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;
    }
}
于 2013-02-20T11:16:41.617 回答
0

我也收到了这个问题:

android.view.InflateException: Binary XML file line #2: addView(View, LayoutParams) is not supported in AdapterView 原因:java.lang.UnsupportedOperationException: addView(View, LayoutParams) 在 AdapterView 中不受支持

我在充气时忘记添加“false”.xml。添加后它可以工作。我的代码短语如下。我LinearLayout为我的.xml.

if(convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_format,parent,false);
    }
于 2021-01-31T18:45:56.953 回答