gpt4 book ai didi

java - Android 上下文菜单未出现

转载 作者:行者123 更新时间:2023-12-01 14:16:14 25 4
gpt4 key购买 nike

我的问题正如描述中所述,用户“触摸并按住”时不会出现上下文菜单。我有一种感觉,它可能是我放置 registerForContextMenu 的地方。
这是我的主要 Activity :

import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerForContextMenu(getListView());
setContentView(R.layout.activity_main);
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
}

最佳答案

尝试交换这两行

registerForContextMenu(getListView());
setContentView(R.layout.activity_main);

应该是这样

setContentView(R.layout.activity_main);
registerForContextMenu(getListView());

你的感觉是正确的。您的 ListView 位于您的 layout 中,因此您需要先对其进行充气,然后再注册 View

关于java - Android 上下文菜单未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18087713/

25 4 0