java - 尝试膨胀布局时适配器出现异常

标签 java android android-listview android-arrayadapter android-adapter

我正在尝试在适配器的 getView 方法中膨胀布局:

这是错误:

01-18 20:58:04.873: E/AndroidRuntime(21583): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.widget.AdapterView.addView(AdapterView.java:478)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at com.myname.myapp.gui.MyAdapter.getView(MyAdapter.java:46)

错误(根据eclipse发生在converView =mInflater...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        convertView = mInflater.inflate(
                android.R.layout.simple_list_item_1, parent);
    }
    ((TextView) convertView.findViewById(R.id.text1)).setText(titles
            .get(position));
    return convertView;
}

这是我的适配器的构造函数:

public MyAdapter(ArrayList<String> list, Context context) {
        titles = list;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

最佳答案

最好的方法是

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);

是的,您应该传递实际的父级,因为布局可能会影响膨胀中使用的参数。第三个参数表示inflate方法是否应将 View 添加到父 View 。

默认情况下,它会将 View 添加到父级,在这种情况下,您希望返回 View 但不将其添加到父级。

您可以在这里阅读:

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(org.xmlpull.v1.XmlPullParser , android.view.ViewGroup, boolean 值)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters:

resource ID for an XML layout resource to load (e.g., R.layout.main_page)

root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

关于java - 尝试膨胀布局时适配器出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212372/

相关文章:

android - 如何通过有线连接使用 SpaceDesk?

java - 选择微调项目值

android - 坚持使用 xmlpullparser 读取 Xml

java - 带有自定义适配器的 ListView 不显示所有项目

Android:屏幕旋转后 ListView 双条目

java - Windows 中 Java 和 C 之间的任何 IPC 机制——不需要套接字

java - 更新始终位于顶部的 Android View

java - resolveClass 不解析符号引用

android - 带有RadioButton单选的自定义ListView

java - spectj 不拦截带有注释的方法