开发了一个简单的功能:
长按ListView中的item后, 弹出选择菜单:
public class NetworktoolActivity extends Activity implements View.OnClickListener, AdapterView.OnItemLongClickListener{ boolean isSendItem = false; int selectPos = -1; @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { isSendItem = tranAdapter.isSendItem(position); selectPos = position; registerForContextMenu(view); openContextMenu(view); unregisterForContextMenu(view); return false; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("COPY"); if(isSendItem)menu.add("RETRY"); else menu.add("SAVE AS FILE"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { if(item.getTitle().equals("COPY")){ ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); cm.setPrimaryClip(ClipData.newPlainText(TAG, tranAdapter.getContent(selectPos))); }else if(item.getTitle().equals("RETRY")){ etUrl.setText(tranAdapter.getContent(selectPos)); onClick(btGo); }else if(item.getTitle().equals("SAVE AS FILE")){ saveAsFile(tranAdapter.getItem(selectPos)); } return true; }
出了异常:
02-08 14:26:04.733 11061-11061 E/AndroidRuntime: FATAL EXCEPTION: main java.lang.StackOverflowError at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856) at android.view.View.showContextMenu(View.java:4277) at android.app.Activity.openContextMenu(Activity.java:2879) at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89) at android.
修正代码如下:
public class NetworktoolActivity extends Activity implements View.OnClickListener, AdapterView.OnItemLongClickListener{ String TAG = "NetworktoolActivity"; EditText etUrl; Button btGo; ListView lvInfos; TransactionAdapter tranAdapter; H h; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_networktool); init(); } void init(){ etUrl = (EditText)findViewById(R.id.etUrl); btGo = (Button)findViewById(R.id.btGo); btGo.setOnClickListener(this); lvInfos = (ListView)findViewById(R.id.lvInfo); lvInfos.setOnItemLongClickListener(this); tranAdapter = new TransactionAdapter(this); lvInfos.setAdapter(tranAdapter); //关键CODE registerForContextMenu(lvInfos); } @Override public void onClick(View v) { if(v == btGo){ startNewRequest(etUrl.getText().toString()); } } void startNewRequest(final String url){ new Thread(){ @Override public void run() { Transaction t = new Transaction(); t.id = t.hashCode(); t.url = url; t.reqTime = TimeUtils.getTimeString(); t.res = "waiting..."; Message msg = h.obtainMessage(H.MSG_ADD, t); h.sendMessage(msg); byte[] content = HttpUtilsAndroid.getByteContentFromURL(url); t.resTime = TimeUtils.getTimeString(); t.rawData = content; t.res = content != null ? new String(content) : "ERROR"; h.sendEmptyMessage(H.MSG_UPDATE); } }.start(); } boolean isSendItem = false; int selectPos = -1; @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { isSendItem = tranAdapter.isSendItem(position); selectPos = position; //----------递归来源---------- //registerForContextMenu(view); //openContextMenu(view); //unregisterForContextMenu(view); return false; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("COPY"); if(isSendItem)menu.add("RETRY"); else menu.add("SAVE AS FILE"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { if(item.getTitle().equals("COPY")){ ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); cm.setPrimaryClip(ClipData.newPlainText(TAG, tranAdapter.getContent(selectPos))); }else if(item.getTitle().equals("RETRY")){ etUrl.setText(tranAdapter.getContent(selectPos)); onClick(btGo); }else if(item.getTitle().equals("SAVE AS FILE")){ saveAsFile(tranAdapter.getItem(selectPos)); } return true; }