Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:20:18+00:00 2026-06-04T02:20:18+00:00

I am implementing Expandable List view in android and i am getting the above

  • 0

I am implementing Expandable List view in android and i am getting the above titled error. Please help me.

Main activity is –

package com.expand;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;


public class MyExpandableListViewActivity extends Activity {
    /** Called when the activity is first created. */



    static final String groupElements[]= {
           "India",
           "Australia",
           "England",
           "South Africa"
         };

    static final String[][] childElements=
    {
           {
          "Sachin Tendulkar",
          "Raina",
          "Dhoni",
          "Yuvi"
           },
           {
          "Ponting",
          "Adam Gilchrist",
          "Michael Clarke"
           },
           {
          "Andrew Strauss",
          "kevin Peterson",
          "Nasser Hussain"
           },
           {
          "Graeme Smith",
          "AB de villiers",
          "Jacques Kallis"
           }
            };



    DisplayMetrics metrics;
    int width;
    ExpandableListView expandList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        expandList = (ExpandableListView)findViewById(R.id.expandList1);
        metrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;

        //ExpAdapter adapter = new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements);

        expandList.setAdapter(new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements));
        expandList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));


        expandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                // TODO Auto-generated method stub

                 Log.e("onGroupExpand", "OK");
            }
        });

        expandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                // TODO Auto-generated method stub

                Log.e("onGroupCollapse", "OK");

            }
        });

        expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {



            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                //getting the item that is selected
                //String s = (String) expandList.getItemAtPosition((int) id);

                Toast.makeText(MyExpandableListViewActivity.this, "You have selected :"  , Toast.LENGTH_SHORT).show();

                return false;
            }
        });

    }



    public int GetDipsFromPixel(float pixels)
    {
        // Get the screen's density scale
        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);
    }


}

ExpAdapter class is –
I have implemented the adapter in other class and have called it in mt main activity

package com.expand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;



    public class ExpAdapter extends BaseExpandableListAdapter {

        public Context myContext;
        public String[][] childElements;
        TextView childValues;
        public String[] groupElements;


        public ExpAdapter(Context context, String[] group, String[][] childs)
        {

            this.myContext=context;
            this.groupElements = group;
            this.childElements = childs;

        }



        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childElements[groupPosition][childPosition];
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub

            return 0;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null){

                LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView  = inflator.inflate(R.layout.child_rows, parent);

            }
            childValues = (TextView)convertView.findViewById(R.id.rowValues);
            childValues.setText(childElements[groupPosition][childPosition]);

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return groupElements[groupPosition].length();
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return groupElements[groupPosition];
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return groupElements.length;
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null){
                LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.group_rows, null);
            }
            TextView group = (TextView)convertView.findViewById(R.id.groupId);
            group.setText(groupElements[groupPosition]);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }




    }

main.xml-

this is the xnl that is displayed at the first by the main activity

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >



        <ExpandableListView 
            android:id="@+id/expandList1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >


                 <TextView 
                android:id="@+id/android:empty"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
            </TextView>


        </ExpandableListView>


    </LinearLayout>

group_row.xml

this is the xml for the group elements

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gropu_name"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="vertical" >


        <TextView 
            android:id="@+id/groupId"
            android:layout_height="40dp"
            android:layout_width="wrap_content"
            android:paddingLeft="30dp"
            android:gravity="center_vertical"
            android:textSize="16dp"
            android:textStyle="bold"
            />

    </LinearLayout>

child_row.xml
this is the xml for the child elements

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/rowValues"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:paddingLeft="50dp"
        android:textSize="12dp" />


</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:20:19+00:00Added an answer on June 4, 2026 at 2:20 am

    Seems like Adapterview does not allow adding new view,
    I encountered same problem

    Solve it by replacing following line

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

    to

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

    UPDATE

    Instead of not using a parent at all, you should simply tell the Inflater not to attach the inflated view to the parent with

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

    See also this answer.

    The reason is that adapter takes care of attaching views to parent itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing Document-View pattern I faced the problem: standard generic collections and dictionaries ( List<T>
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I am trying to implement a ExpandableListView in Android by implementing a custom adapter
There are no controls in Android that provide Tree-like View. There is an ExpandableList
When implementing SuperBoxSelect (http://www.sencha.com/forum/showthread.php?69307-3.x-Ext.ux.form.SuperBoxSelect), I've realized that it currently does not support shift +
Implementing a PhoneGap app for Android and iOS, the app is using Facebook Connect
I have an expandable list using a simplecursortreeadapter How would I integrate an onclicklistener
Implementing org.jscep.server.ScepServlet I need to provide an implementation for the method doEnroll ( List<X509Certificate>
Im implementing a video playback in android im completely new to android, and this
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.