我的自定义微调器和微调器适配器有问题。它在我启动后崩溃。我想在我的微调器上更改文本的颜色和背景颜色,所以我创建了一个XML文件my_spinner.xml,它看起来像这样:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/White"
android:background="@color/Blue"
android:id="@+id/spinner_textView" >
</TextView>
</LinearLayout>
然后我创建了一个MyAdapter类,如下所示: 公共类ProjectAdapter扩展了ArrayAdapter {
public ProjectAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}
public View getCustomView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.my_spinner, parent, false);
TextView tv = (TextView) findViewById(R.id.spinner_textView);
tv.setText(projectList.get(position));
return row;
}
public View getDropDownView(int position, View convertView,ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
}
我在那里使用ProjectAdapter:
spinnerProject.setAdapter(new ProjectAdapter(this, R.layout.my_spinner, projectList));
任何人都可以告诉我,我做错了什么?谢谢你的帮助
答案 0 :(得分:0)
您需要更改此
View row = inflater.inflate(R.layout.my_spinner, parent, false);
TextView tv = (TextView) findViewById(R.id.spinner_textView);
到
View row = inflater.inflate(R.layout.my_spinner, parent, false);
TextView tv = (TextView)row.findViewById(R.id.spinner_textView);
其中一个错误。没有堆栈跟踪,很难跟踪错误。如果您还有其他问题,建议您发布stacktrace