以下のコードは私にとって非常にうまく機能しています。
String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", };
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.btnwillappear);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ListPopupWindow lpw = new ListPopupWindow(Test_listpopActivity.this);
lpw.setAdapter(new ArrayAdapter(Test_listpopActivity.this, android.R.layout.simple_list_item_1, listItems));
lpw.setAnchorView(findViewById(R.id.txtwillappear));
lpw.setWidth(150);
lpw.setHeight(300);
lpw.show();
}
});
}
考えられる問題は 2 つあります。1 つ目はアダプタで、2 つ目はコードが実行される場所です。後者の場合、コードが onCreate に含まれていないことを確認してください。