Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ExpandableListView的ContextMenu项上的ClassCastException_Android - Fatal编程技术网

Android ExpandableListView的ContextMenu项上的ClassCastException

Android ExpandableListView的ContextMenu项上的ClassCastException,android,Android,你好,我对ExpandableListView上的上下文菜单有问题。我遵循了android网站上的例子,但在这一行我得到了ClassCastException String title = ((TextView)info.targetView).getText().toString(); 这是我的代码,我只发布主要部分 private class expandableList extends SimpleExpandableListAdapter { public expanda

你好,我对ExpandableListView上的上下文菜单有问题。我遵循了android网站上的例子,但在这一行我得到了ClassCastException

String title = ((TextView)info.targetView).getText().toString();
这是我的代码,我只发布主要部分

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

}
如果您有任何帮助,我们将不胜感激,谢谢

编辑:这是RecentDevice类的其余部分,这里的所有内容都包含在其中,包括expandableList类:

公共类RecentDevices扩展了ExpandableListActivity{ 公共静态字符串TAG=“HomeShare”

TextView设备名称;
图像查看设备;
观点五;
//expandablelistview的变量
最终字符串Name=“Name”;
最终字符串Image=“Image”;
//上下文菜单顺序的静态变量
静态最终int MENU_HOME=MENU.FIRST;
最终ArrayList headerData=新ArrayList();
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.最近的_设备);
LayoutInflater LayoutInflater=(LayoutInflater)this.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
//expandablelistview中标题和子视图的ArrayList
//最终ArrayList headerData=新ArrayList();
final ArrayList childData=新ArrayList();
//向标题添加数据
final HashMap group1=新HashMap();
组1.put(名称,“家用设备”);
headerData.add(第1组);
final HashMap group2=新HashMap();
组2.put(名称,“工作设备”);
headerData.add(第2组);
//为子视图中的数据初始化Hashmap
最终ArrayList group1data=新ArrayList();
添加(group1data);
最终ArrayList group2data=新ArrayList();
childData.add(group2data)
final HashMap map=新HashMap();
地图放置(名称“计算机”);
group1data.add(map);
}
}
//添加(group1data);
//将数据添加到适配器并显示
Log.d(标记“set expandlistview”);
expandableList expand_list=新的expandableList(此为headerData,
android.R.layout.simple_可扩展_列表_项_1,新字符串[]{Name},新int[]{android.R.id.text1},
(列表>>)childData,0,null,新int[]{});
Log.d(标记“set expandlistview completed”);
setListAdapter(展开列表);
registerForContextMenu(getExpandableListView());
}

您的ClassCastException发生在
最近的设备中
第200行:

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

android文档声明:

“公共视图targetView 自:API级别1

显示关联菜单的视图。这将是此ExpandableListView的子视图之一。“

ExpandableListView的子视图不是文本视图,而是封装整个视图的线性布局。您很可能需要获取整个LinearLayout视图,然后向下钻取您要查找的文本视图

大概是这样的:

String title = ((TextView) info.targetView.findViewById(R.id.search_device_id)).getText().toString();

是的,我知道。这就是我在问题中所说的路线。但我不知道该怎么办。对我来说,没有什么是与众不同的。似乎
RecentDevices
类是相关的,但你没有在这里显示它?我已经添加了RecentDevices类的剩余代码,我之前发布的所有内容都嵌套在其中。我只是觉得这部分没有必要,这就是为什么我写了它们,这样更容易阅读。再次感谢你1.非常感谢。避免了轻微的头痛。太棒了!:)
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());
}
09-15 17:17:41.733: ERROR/AndroidRuntime(4729):at com.AappDevs.BlueShare.RecentDevices.onContextItemSelected(RecentDevices.java:200)
String title = ((TextView) info.targetView.findViewById(R.id.search_device_id)).getText().toString();