尝试为内部列表视图的每一行膨胀不同的视图 (android.view.InflateException: Binary XML..)

Posted

技术标签:

【中文标题】尝试为内部列表视图的每一行膨胀不同的视图 (android.view.InflateException: Binary XML..)【英文标题】:Trying to inflate different views for each row of a list view inside getting this (android.view.InflateException: Binary XML..) 【发布时间】:2015-11-03 16:43:51 【问题描述】:

(如果您愿意,我可以提供任何其他信息)

当我尝试为ListView 中的每一行设置单独的布局时出现此错误。我有一个从BaseAdapter 扩展的自定义适配器,它实现了View.OnClickListener()

我只会给出导致错误的getView()函数

@Override
public View getView(int position, View convertView, ViewGroup parent) 
    LayoutInflater inflater = (LayoutInflater) listContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = null;
    if(list.get(position).type=="1")
    
        row = inflater.inflate(R.layout.others, parent,false); //this line causes error 
    
    else
    
       row = inflater.inflate(R.layout.others, parent,false);  //same as above so the cause of the error again
       

    return row;


这里是others.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="@color/feed_bg"
    android:orientation="vertical">
    <!--<TextView-->
        <!--android:layout_-->
        <!--android:layout_-->
        <!--android:text="this is other than than stream"/>-->

    <LinearLayout
        android:layout_
        android:layout_

        android:layout_marginLeft="@dimen/feed_item_margin"
        android:layout_marginRight="@dimen/feed_item_margin"
        android:layout_marginTop="@dimen/feed_item_margin"
        android:background="@drawable/bg_parent_rounded_corner"
        android:orientation="vertical"
        android:paddingBottom="@dimen/feed_item_padding_top_bottom"
        android:paddingTop="@dimen/feed_item_padding_top_bottom" >

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="horizontal"
            android:paddingLeft="@dimen/feed_item_padding_left_right"
            android:paddingRight="@dimen/feed_item_padding_left_right" >

            <ImageView
                android:id="@+id/profilePic"
                android:layout_
                android:layout_
                android:scaleType="fitCenter"
                android:background="@drawable/image_for_profile_pic">
            </ImageView>

            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="vertical"
                android:paddingLeft="@dimen/feed_item_profile_info_padd" >

                <TextView
                    android:text="Usman Khan"
                    android:id="@+id/name"
                    android:layout_
                    android:layout_
                    android:textSize="@dimen/feed_item_profile_name"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/timestamp"
                    android:layout_
                    android:layout_
                    android:textColor="#6f6f6f"
                    android:textSize="@dimen/feed_item_timestamp"
                    android:text="2 days ago"/>
            </LinearLayout>
        </LinearLayout>



        <TextView
            android:text="the intellectual and practical activity encompassing the systematic study of the structure and behaviour of the physical and natural world through observation and experiment."
            android:textColor="#6f6f6f"
            android:id="@+id/txtStatusMsg"
            android:layout_
            android:layout_
            android:paddingBottom="5dp"
            android:paddingLeft="@dimen/feed_item_status_pad_left_right"
            android:paddingRight="@dimen/feed_item_status_pad_left_right"
            android:paddingTop="@dimen/feed_item_status_pad_top" />
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_
            android:layout_
            android:id="@+id/progressBar"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:progress="80"


            />

        <!--<TextView-->
        <!--android:id="@+id/txtUrl"-->
        <!--android:layout_-->
        <!--android:layout_-->
        <!--android:linksClickable="true"-->
        <!--android:paddingBottom="10dp"-->
        <!--android:paddingLeft="@dimen/feed_item_status_pad_left_right"-->
        <!--android:paddingRight="@dimen/feed_item_status_pad_left_right"-->
        <!--android:textColorLink="@color/link" />-->

        <SeekBar
            android:layout_
            android:layout_
            android:id="@+id/seekBar"
            android:layout_marginLeft="5dp"/>

        <!--<ImageView-->

        <!--android:id="@+id/feedImage1"-->
        <!--android:layout_-->
        <!--android:layout_-->
        <!--android:background="@drawable/pic2"-->
        <!--android:scaleType="fitXY"-->
        <!--android:visibility="visible" />-->



        <LinearLayout
            android:background="#d8d7e1"
            android:orientation="vertical"
            android:layout_
            android:layout_
            android:layout_gravity="center_horizontal"
            android:layout_margin="4dp">
            <TextView
                android:text="View all comments.."
                android:id="@+id/viewAllCommentsTextView"
                android:layout_
                android:layout_
                android:textSize="18dp"
                android:layout_marginLeft="15dp"
                android:textColor="#5d5d5d"
                android:layout_marginBottom="5dp"
                android:clickable="true"/>
            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="horizontal"
                android:paddingLeft="@dimen/feed_item_padding_left_right"
                android:paddingRight="@dimen/feed_item_padding_left_right"
                android:layout_marginBottom="10dp">

                <ImageView
                    android:id="@+id/comment1pic"
                    android:layout_
                    android:layout_
                    android:scaleType="fitCenter"
                    android:background="@drawable/image_for_comment_pic"
                    >
                </ImageView>
                <TextView
                    android:layout_marginLeft="5dp"
                    android:text="bey man come on whats up..... jut give it up already man you kiddingme? ok i will see man"
                    android:layout_
                    android:layout_
                    android:textColor="#6f6f6f"/>

            </LinearLayout>
            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="horizontal"
                android:paddingLeft="@dimen/feed_item_padding_left_right"
                android:paddingRight="@dimen/feed_item_padding_left_right"
                android:layout_marginBottom="10dp">

                <ImageView
                    android:id="@+id/comment2pic"
                    android:layout_
                    android:layout_
                    android:scaleType="fitCenter"
                    android:background="@drawable/image_for_comment_pic">
                </ImageView>
                <TextView
                    android:textColor="#6f6f6f"
                    android:layout_marginLeft="5dp"
                    android:text="ok so what you are saying that "
                    android:layout_
                    android:layout_ />

            </LinearLayout>
            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="horizontal"
                android:paddingLeft="@dimen/feed_item_padding_left_right"
                android:paddingRight="@dimen/feed_item_padding_left_right"
                android:layout_marginBottom="10dp">

                <ImageView
                    android:id="@+id/comment3pic"
                    android:layout_
                    android:layout_
                    android:scaleType="fitCenter">
                </ImageView>
                <TextView
                    android:layout_marginLeft="5dp"
                    android:textColor="#6f6f6f"
                    android:layout_
                    android:layout_ />

            </LinearLayout>







        </LinearLayout>

    </LinearLayout>

</LinearLayout>

这是日志:

08-11 17:25:45.878  25051-25051/com.rateker.newratekerwithnevdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:613)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at com.rateker.newratekerwithnevdrawer.VivzAdapter.getView(Item1Activity.java:218)
        at android.widget.AbsListView.obtainView(AbsListView.java:2207)
        at android.widget.ListView.makeAndAddView(ListView.java:1860)
        at android.widget.ListView.fillDown(ListView.java:687)
        at android.widget.ListView.fillFromTop(ListView.java:748)
        at android.widget.ListView.layoutChildren(ListView.java:1676)
        at android.widget.AbsListView.onLayout(AbsListView.java:2037)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:562)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14099)
        at android.view.ViewGroup.layout(ViewGroup.java:4464)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
        at android.view.Choreographer.doCallbacks(Choreographer.java:579)
        at android.view.Choreographer.doFrame(Choreographer.java:548)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newI

【问题讨论】:

您是否要在该 XML 上加载大图像文件? 不,我实际上不是 【参考方案1】:

如日志所示,问题出在标签 XML 文件第 2 行:

我换了

android:background="@color/feed_bg"

到,

android:background="#d3d6db"

以及所有引用“@color/”的地方 我不知道为什么会出现这个问题,但我不得不用确切的颜色替换所有内容,例如“#ffffff”而不是“@color/white”

【讨论】:

【参考方案2】:

您不能使用“=”运算符比较字符串:

if (convertView == null) 
    if(list.get(position).getType.equals("1"))
        
            row = inflater.inflate(R.layout.others, parent,false);
        
        else
        
           row = inflater.inflate(R.layout.others, parent,false); 
           

【讨论】:

【参考方案3】:

检查第一次时间视图是否为空,然后使用 if 条件。 并简单地检查位置而不是 list.get(position).type=="1" 并且您可以使用单个 xml 文件中的两种布局在位置检查时隐藏一种布局

【讨论】:

【参考方案4】:

尝试将您的功能更改为:

@Override
    public View getView(int position, View convertView, ViewGroup viewGroup) 
        if (convertView == null) 
            LayoutInflater mInflater = (LayoutInflater)
                listContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            if (list.get(position).type.equals("1")) 
                convertView = mInflater.inflate(R.layout.others, null);
            
            else 
                // Do what you want to do in the else.
            
        

        // Do what you need to do with the view.

        return convertView;

P.S.:就像上面所说的,你必须使用equals()来比较String

【讨论】:

我得到了这个新错误:E/AndroidRuntime:致命异常:main java.lang.NullPointerException......我此时检查了列表的大小,它是“5”,所以它被初始化了 一点也不。我已将context_ 更改为您的listcontext。但即便如此,它还是抛出了 NPE,请给我完整的日志。 哦。好的。那你能给我看看完整的日志吗? P.S.:修正了另一个错字。 getType 代表 typeif 语句中。 我通过密集调试自己解决了...检查答案

以上是关于尝试为内部列表视图的每一行膨胀不同的视图 (android.view.InflateException: Binary XML..)的主要内容,如果未能解决你的问题,请参考以下文章

膨胀内部类视图时出错

列表视图中的重叠行

为 UITableView 的每一行推送不同的 UIView

UISplitView - 为 masterView 中的每一行加载不同的 detailView

Django 管理员。当对象具有布尔字段== True时,如何为列表视图中的每一行添加背景颜色?

用于从布局中膨胀视图的 iOS 模拟 (xib)