3

(I can provide any other info if you want)

I am getting this error when I try to set separate layout for each row in a ListView. I have a custom adapter extended from the BaseAdapter which implements View.OnClickListener()

I will only give the getView() function which causes the error

@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;

}

Here is the others.xml

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        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_width="fill_parent"
            android:layout_height="wrap_content"
            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_width="@dimen/feed_item_profile_pic"
                android:layout_height="@dimen/feed_item_profile_pic"
                android:scaleType="fitCenter"
                android:background="@drawable/image_for_profile_pic">
            </ImageView>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="@dimen/feed_item_profile_info_padd" >

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

                <TextView
                    android:id="@+id/timestamp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    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_width="fill_parent"
            android:layout_height="wrap_content"
            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_width="fill_parent"
            android:layout_height="wrap_content"
            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_width="fill_parent"-->
        <!--android:layout_height="wrap_content"-->
        <!--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_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/seekBar"
            android:layout_marginLeft="5dp"/>

        <!--<ImageView-->

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



        <LinearLayout
            android:background="#d8d7e1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:layout_margin="4dp">
            <TextView
                android:text="View all comments.."
                android:id="@+id/viewAllCommentsTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:layout_marginLeft="15dp"
                android:textColor="#5d5d5d"
                android:layout_marginBottom="5dp"
                android:clickable="true"/>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                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_width="25dp"
                    android:layout_height="25dp"
                    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_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#6f6f6f"/>

            </LinearLayout>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                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_width="25dp"
                    android:layout_height="25dp"
                    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_width="fill_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                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_width="25dp"
                    android:layout_height="25dp"
                    android:scaleType="fitCenter">
                </ImageView>
                <TextView
                    android:layout_marginLeft="5dp"
                    android:textColor="#6f6f6f"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>







        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Here is the LOG:

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
Mauker
  • 11,237
  • 7
  • 58
  • 76
Nyob
  • 416
  • 1
  • 3
  • 14

4 Answers4

1

As it is shown in the log, the problem was in the tags XML file line #2:

i replaced

android:background="@color/feed_bg"

to,

android:background="#d3d6db"

and at all the places where there was a reference to the "@color/" I don't know why there was this problem but i had to replace all with the exact color e.g. "#ffffff" instead of "@color/white"

Nyob
  • 416
  • 1
  • 3
  • 14
0

Check first Time View is Null and after that use if condition. and simply check position instead of list.get(position).type=="1" and you can use two layout from single xml file hide one layout on position check

Anil
  • 1,087
  • 1
  • 11
  • 24
0

Try changing your function to this:

@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.: Like it was said, you have to use equals() to compare String.

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • i got this new error: E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException...... i checked the size of the list at this time its '5' so its initialized – Nyob Aug 11 '15 at 13:37
  • Not at all. I've changed `context_` to your `listcontext`. But if even so it's throwing a NPE, give me the full log. – Mauker Aug 11 '15 at 13:43
  • Oh. Ok. Then could you please show me the full log? P.S.: Fixed another typo. `getType` for `type` inside the `if` statement. – Mauker Aug 11 '15 at 13:44
  • i solved it my self through intense debugging... check the answer – Nyob Aug 12 '15 at 05:14
0

You can't compare String using "=" operator :

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); 
        }   
}
SANAT
  • 8,489
  • 55
  • 66