触摸微调器时应用程序崩溃
App crashes when spinner is touched
首先,我是第一次来这里,如果有失礼请见谅。
我正在制作一个简单的微调器。它读取位于 string.xml
中的字符串数组,然后将国家代码与 select 来自可绘制文件夹(所有图像都位于适当位置)的适当图像进行比较。一切正常,似乎微调器加载了信息,但是当我触摸它显示时,应用程序崩溃了。
如果有人能帮助我将不胜感激
适配器Class:
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;
}
}
主要Class:
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,没什么特别的。
我已经坚持了好几天了,更糟糕的是我已经有其他旋转器在工作了。
使用下面的代码而不是您的适配器代码,因为您正在使用扩展简单的 ArrayAdapter 图像使用 BaseAdapter。
public class CustomAdapterPhone2 extends BaseAdapter {
String[] countries;
private Context context;
public CustomAdapterPhone2(Context contexts, int resource, String[] objects) {
/*super(context, resource, objects);*/
this.context = contexts;
this.countries = context.getResources().getStringArray(R.array.CountryCodes);
}
@Override
public int getCount() {
return countries.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.custom_spinner_items_phone, parent, false);
TextView label = (TextView) convertView.findViewById(R.id.custom_phone_textview);
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(context.getResources().getIdentifier(aux2, "drawable", context.getPackageName()));
return convertView;
}
}
我测试过它工作正常。
首先,我是第一次来这里,如果有失礼请见谅。
我正在制作一个简单的微调器。它读取位于 string.xml
中的字符串数组,然后将国家代码与 select 来自可绘制文件夹(所有图像都位于适当位置)的适当图像进行比较。一切正常,似乎微调器加载了信息,但是当我触摸它显示时,应用程序崩溃了。
如果有人能帮助我将不胜感激
适配器Class:
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;
}
}
主要Class:
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,没什么特别的。 我已经坚持了好几天了,更糟糕的是我已经有其他旋转器在工作了。
使用下面的代码而不是您的适配器代码,因为您正在使用扩展简单的 ArrayAdapter 图像使用 BaseAdapter。
public class CustomAdapterPhone2 extends BaseAdapter {
String[] countries;
private Context context;
public CustomAdapterPhone2(Context contexts, int resource, String[] objects) {
/*super(context, resource, objects);*/
this.context = contexts;
this.countries = context.getResources().getStringArray(R.array.CountryCodes);
}
@Override
public int getCount() {
return countries.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.custom_spinner_items_phone, parent, false);
TextView label = (TextView) convertView.findViewById(R.id.custom_phone_textview);
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(context.getResources().getIdentifier(aux2, "drawable", context.getPackageName()));
return convertView;
}
}
我测试过它工作正常。