任务是用 POJO 对象中的值填充微调器。在此之前,我正在检查正常的字符串值和 BaseAdapter。
我在活动组中有一项活动。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_filter);
startMonth = (Spinner) findViewById(R.id.startMonth);
startMonth.setAdapter(new CustomSpinnerAdapter(getParent(), new ArrayList<String>()));
}
和 layout_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/spinnerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
我的适配器类是:
public class CustomSpinnerAdapter extends BaseAdapter{
private List<String> alertList;
private LayoutInflater mInflater;
public CustomSpinnerAdapter(Context context, Object results) {
alertList = (List<String>) results;
//just check if it works.
//in real, actual object fits in
alertList.add("January");
alertList.add("Feburary");
alertList.add("March");
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return alertList.size();
}
@Override
public Object getItem(int position) {
return alertList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_spinner_row, null);
holder = new ViewHolder();
holder.spinnerValue = (TextView) convertView.findViewById(R.id.spinnerValue);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.spinnerValue.setText(alertList.get(position));
return convertView;
}
static class ViewHolder {
TextView spinnerValue; //spinner name
}
}
当我单击微调器时,出现以下错误:
E/AndroidRuntime(1686): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4135fa38 is not valid; is your activity running?
我找不到该问题的任何有效答案。有什么想法吗?
提前致谢!