2

任务是用 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?

我找不到该问题的任何有效答案。有什么想法吗?

提前致谢!

4

2 回答 2

3

您需要将视图设置为相应的视图对象,以使微调器正常工作。为此,您需要更改活动的 oncreate:

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_date_filter, null); 
this.setContentView(viewToLoad); 

完整解释:http: //developer.android.com/reference/android/view/LayoutInflater.html

于 2012-11-05T13:03:00.487 回答
1

从您的适配器代码中,我可以说您尚未初始化LayoutInflater并且不这样做您正在尝试膨胀布局。所以在你的构造函数中,使用:

 this.mInflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
于 2012-11-05T13:17:27.313 回答