我开始使用一些fragments
开发应用。在一个fragment
中,我希望在ScrollView
中使用LinearLayout
显示通过UDP接收的数据。数据集的数量是不规则的。当我长时间点击一个条目时,应该有一个弹出菜单。
工作原理:
- 收到包
- 在ScrollView
。
什么不起作用:
- 打开弹出菜单
这里是代码:
public void updateList(final String received){
received_parts_hash = received.split("#");
// clear layout everytime a new string is received
if(linearLayout.getChildCount() > 0) {
linearLayout.removeAllViews();
}
for (int i = 1; i<received_parts_hash.length; i++){
String[] received_parts = received_parts_hash[i].split(";");
myTv = new TextView(getActivity());
String myString = received_parts[0] + ", " + received_parts[1] + ", " + received_parts[2];
myTv.setText(myString);
linearLayout.addView(myTv);
registerForContextMenu(myTv);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==myTv.getId()) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
String information = received_parts_hash[info.position+1];
String[] information_parts = information.split(";");
menu.setHeaderTitle("info");
menu.add(information_parts[3]);
menu.add(information_parts[4]);
menu.add(information_parts[5]);
}
}
我长按一个项目时会调用 onCreateContextMenu()
,但menuInfo始终为null,因此应用程序崩溃。
有什么建议吗?