【问题标题】:App crashes when spinner is touched触摸微调器时应用程序崩溃
【发布时间】: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


【解决方案1】:

使用以下代码而不是您的适配器代码,因为您正在使用扩展简单的 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;
    }
}

我已经测试过它工作正常。

【讨论】:

  • 感谢您的努力,但我已经设法自己找到了解决方案。反正我给你正确答案
  • 而且,ArrayAdapter“应该”和 BaseAdapter 一样好用,因为 ArrayAdapter 从 BaseAdapter 扩展而来
猜你喜欢
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 2022-01-14
  • 1970-01-01
  • 2013-01-05
相关资源
最近更新 更多