开发者

ClassCastException on ContextMenu Item of ExpandableListView

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/AndroidRuntim开发者_运维知识库e(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());
}


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();


Your ClassCastException happens in RecentDevices line 200:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜