我想在長時間按下按鈕時創建浮動上下文菜單。我已經閱讀了所有的答案,但仍然是這樣,我正在讓自己瘋狂。這是我的代碼:Android中的浮動上下文菜單
R.menu.menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/MnuOpc1" android:title="Opcion1"
android:icon="@drawable/ic_launcher"></item>
<item android:id="@+id/MnuOpc2" android:title="Opcion2"
android:icon="@drawable/ic_launcher"></item>
<item android:id="@+id/MnuOpc3" android:title="Opcion3"
android:icon="@drawable/ic_launcher"></item>
</menu>
OnCreateContextMenu(...)
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
onContextItemSelected(...)
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("Hello...","I got the switch");
switch (item.getItemId()) {
case R.id.MnuOpc1:
Log.v("Hello...","Option 1");
return true;
case R.id.MnuOpc2:
Log.v("Hello...","Option 2");
return true;
default:
return super.onContextItemSelected(item);
}
我認爲上面的所有代碼都是正確的,但在registerForContextMenu中調用時我缺少一些東西:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linear = (LinearLayout) findViewById(R.layout.main);
registerForContextMenu(linear);
我試過registerForContextMenu(getListView());
但它不起作用,我從Eclipse中收到錯誤。我究竟做錯了什麼?
從Eclipse獲得什麼錯誤? – Nate