4
我想在按鈕單擊時顯示ListPopupWindow中顯示一個簡單的字符串數組。然而,我遇到了問題,因爲當我做一些ArrayAdapter<String>
或定製的適配器的最小設置時,當我去顯示彈出窗口時,我遇到了資源未發現異常。這裏是我正在使用的代碼(在它之後的堆棧跟蹤)。關於發生了什麼的任何想法?如何獲得ListPopupWindow以使用自定義適配器?
public class AndroidSandboxActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showListPopup(btn);
}
});
}
public void showListPopup(View anchor) {
ListPopupWindow popup = new ListPopupWindow(this);
popup.setAnchorView(anchor);
ListAdapter adapter = new MyAdapter(this);
popup.setAdapter(adapter);
popup.show();
}
public static class MyAdapter extends BaseAdapter implements ListAdapter {
private final String[] list = new String[] {"one","two","three"};
private Activity activity;
public MyAdapter(Activity activity) {
this.activity = activity;
}
@Override
public int getCount() {
return list.length;
}
@Override
public Object getItem(int position) {
return list[position];
}
@Override
public long getItemId(int position) {
return position;
}
private static int textid = 1234;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = null;
if (convertView == null) {
LinearLayout layout = new LinearLayout(activity);
layout.setOrientation(LinearLayout.HORIZONTAL);
text = new TextView(activity);
text.setId(textid);
layout.addView(text);
convertView = layout;
} else {
text = (TextView)convertView.findViewById(textid);
}
text.setText(list[position]);
return convertView;
}
}
}
而這裏的堆棧跟蹤(即博格爾斯我想的事情是,它說,它的使用ArrayAdapter
時,我用我自己的自定義適配器):
Thread [<1> main] (Suspended (exception Resources$NotFoundException))
Resources.loadXmlResourceParser(int, String) line: 2047
Resources.getLayout(int) line: 853
PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) line: 389
ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 375
ArrayAdapter.getView(int, View, ViewGroup) line: 366
ListPopupWindow$DropDownListView(AbsListView).obtainView(int, boolean[]) line: 2146
ListPopupWindow$DropDownListView.obtainView(int, boolean[]) line: 1156
ListPopupWindow$DropDownListView(ListView).measureHeightOfChildren(int, int, int, int, int) line: 1261
ListPopupWindow.buildDropDown() line: 1083
ListPopupWindow.show() line: 517
AndroidSandboxActivity.showListPopup() line: 41
AndroidSandboxActivity$1.onClick(View) line: 28
Button(View).performClick() line: 3122
View$PerformClick.run() line: 11942
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 132
ActivityThread.main(String[]) line: 4028
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 491
ZygoteInit$MethodAndArgsCaller.run() line: 844
ZygoteInit.main(String[]) line: 602
NativeStart.main(String[]) line: not available [native method]
任何幫助,將不勝感激!