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 6588767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:05:20+00:00 2026-05-25T17:05:20+00:00

Good day, am having troubles with context menu on an ExpandableListView. i followed the

  • 0

Good day, am having troubles with context menu on an ExpandableListView. i followed the example from the android website, but am getting a ClassCastException on this line.

String title = ((TextView)info.targetView).getText().toString();

this is my code, am posting just the main parts.

private class expandableList extends SimpleExpandableListAdapter {



    public expandableList(Context context,
        List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo,List<? extends List<? extends Map<String, ?>>> childData,int childLayout, String[] childFrom, int[] childTo) {

   super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
            // TODO Auto-generated constructor stub
        }
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){

 final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);   

         ((TextView)v.findViewById(R.id.search_device_id)).setText( (String)((Map<String,Object>)getChild(groupPosition, childPosition)).get(Name));

              return v;
        }

        @Override
        public View newChildView(boolean isLastChild, ViewGroup parent){
return LayoutInflater.from(getApplicationContext()).inflate(R.layout.search_devices, null);
        }   
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo{
            super.onCreateContextMenu(menu, v, menuInfo);

              menu.setHeaderTitle("Devices");
              menu.add(0, MENU_HOME, 0, "Home");         
    }

    public boolean onContextItemSelected(MenuItem menuItem){
     ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)         menuItem.getMenuInfo();

        int groupPosition = 0;
        int childPosition = 0;
        int type = ExpandableListView.getPackedPositionChild(info.packedPosition);

        if(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD){
    groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);
        }       
        switch(menuItem.getItemId()){
        case MENU_HOME: 

            /*classcast exception here on this line*/ 
            String title = ((TextView)info.targetView).getText().toString();    
            Toast.makeText(this, title, Toast.LENGTH_SHORT).show();
        }
        return false;
    }

}

Here is my Search_devices.xml:

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

    <ImageView
    android:id="@+id/device_image_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon">
    </ImageView>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/search_device_id"
    android:gravity="center_horizontal|bottom"
    android:textColor="#ffffff"
    android:padding="2dp">
    </TextView>

</LinearLayout>

and this is the logcat message:

09-15 17:17:41.733: ERROR/AndroidRuntime(4729): java.lang.ClassCastException: android.widget.LinearLayout

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.AappDevs.BlueShare.RecentDevices.onContextItemSelected(RecentDevices.java:200)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at  android.app.Activity.onMenuItemSelected(Activity.java:2254)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2903)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:160)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:885)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:880)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at android.widget.AdapterView.performItemClick(AdapterView.java:284)

09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at android.widget.ListView.performItemClick(ListView.java:3604)

Please any help will be greatly appreciated thanks.

Edit: this is the rest of the RecentDevice class, everything here is contained in it including the expandableList class:

public class RecentDevices extends ExpandableListActivity {
public static String TAG = “HomeShare”;

TextView device_name;
ImageView device_image;
View v;

//variables for expandablelistview
final String Name = "name";
final String Image = "image";

//static variables for context menu order
static final int MENU_HOME = Menu.FIRST;


final ArrayList<HashMap<String,String>> headerData = new ArrayList<HashMap<String,String>>();   

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recent_devices);

LayoutInflater layoutinflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//ArrayList of header and child view in expandablelistview
//final ArrayList<HashMap<String,String>> headerData = new ArrayList<HashMap<String,String>>(); 
final ArrayList<ArrayList<HashMap<String,Object>>> childData = new ArrayList<ArrayList<HashMap<String,Object>>>();

//Add data to header
final HashMap<String,String> group1 = new HashMap<String,String>();
group1.put(Name,"Home devices");
headerData.add(group1);

final HashMap<String, String> group2 = new HashMap<String, String>();
group2.put(Name, "Work devices");
headerData.add(group2);

//initialise Hashmap for data in child view
final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String,Object>>();
childData.add(group1data);

final ArrayList<HashMap<String, Object>> group2data = new ArrayList<HashMap<String,Object>>();
    childData.add(group2data)

              final HashMap<String, Object> map = new HashMap<String,Object>();
                    map.put(Name, "Computers");

              group1data.add(map);

           }
       }

       //childData.add(group1data);

       //add data into adapter and show
       Log.d(TAG, "set expandlistview");
       expandableList expand_list = new expandableList(this, headerData, 
             android.R.layout.simple_expandable_list_item_1, new String[]{Name}, new int[]{android.R.id.text1},
             (List<? extends List<? extends Map<String, ?>>>) childData, 0, null, new int[]{});
    Log.d(TAG, "set expandlistview completed");
   setListAdapter(expand_list);

   registerForContextMenu(getExpandableListView());
}
  • 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-05-25T17:05:21+00:00Added an answer on May 25, 2026 at 5:05 pm

    The android docs state:

    “public View targetView
    Since: API Level 1

    The view for which the context menu is being displayed. This will be one of the children Views of this ExpandableListView.”

    Your childViews for your ExpandableListView aren’t the TextViews, but rather the LinearLayout that encapsulates the whole view. You’ll most likely need to grab that whole LinearLayout view and then drill down to get the TextView you’re looking for.

    Something like this:

    String title = ((TextView) info.targetView.findViewById(R.id.search_device_id)).getText().toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I am having errors in merging/combining/concatenating arrays. For example I have arrays,
Good day, I try to start new Activity from another. But it always crashed.
Good day everybody, I'm having troubles with the display of a data that does
Good day, i am having troubles with the following image below. As you can
Good day, Ive been having a hard time dealing with android notifications lately so
Good day everyone. I have been having the same problem all day at work
Good day everybody! Having the following code: template<typename T, typename OutStream = std::ostream> struct
hope you're having a good day. I have been programming a IRC chatbot in
Good day all, I am having some trouble with image permissions. I am loading
Good day, I'm having a ton of trouble with the following php code, the

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.