【发布时间】:2016-07-26 16:07:54
【问题描述】:
首先,这是我第一次来这里,如果我不礼貌,请原谅我。
我正在制作一个简单的微调器。它读取位于string.xml 的字符串数组,然后比较国家/地区代码以从可绘制文件夹(所有图像都位于该文件夹)中选择合适的图像。一切正常,似乎微调器加载了信息,但是当我触摸它进行显示时,应用程序崩溃了。
如果有人能帮忙,我将不胜感激
适配器类:
public class CustomAdapterPhone2 extends ArrayAdapter<String> {
public CustomAdapterPhone2(Context context, int resource, String [] objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.custom_spinner_items_phone, parent, false);
TextView label=(TextView)convertView.findViewById(R.id.custom_phone_textview);
String [] countries = getContext().getResources().getStringArray(R.array.CountryCodes);
String [] aux = countries[position].split(",");
label.setText(aux[1]);
String aux2 = aux[1].trim().toLowerCase();
// int aux3 = String.valueOf(aux2);
ImageView icon=(ImageView)convertView.findViewById(R.id.custom_phone_image);
icon.setImageResource(getContext().getResources().getIdentifier( aux2, "drawable", getContext().getPackageName()));
return convertView;
}
}
主类:
Spinner spinner_country_code = (Spinner)findViewById(R.id.phone_spinner);
spinner_country_code.setOnItemSelectedListener(this);
CustomAdapterPhone2 customAdapterPhone2 = new CustomAdapterPhone2(getApplicationContext(),R.layout.custom_spinner_items_phone,recourseList);
spinner_country_code.setAdapter(customAdapterPhone2);
微调器布局只有一个textview和一个ImageView,没什么花哨的。 我已经被这个问题困扰了好几天,更糟糕的是我已经有其他的微调器在工作了。
【问题讨论】:
-
提供你的 logcat..
-
在你的适配器类中创建一个 Context 类型的字段,在你的构造函数中设置它然后在你需要的地方使用它而不是 getContext()
-
哪里有问题?!!
标签: android arrays crash spinner