【问题标题】:How to fix Spinner setAdapter() not working?如何修复 Spinner setAdapter() 不起作用?
【发布时间】:2021-01-05 01:38:22
【问题描述】:

当我调用包含此微调器的对话框时,我的 android 应用程序不断崩溃,我确定我的 XML 没问题

我试图通过将代码作为注释来发现问题,结果发现问题出在 setAdapter 行

如果有人可以帮忙,那就太好了。

    CountryManager cm = new CountryManager(this);
    user = new UserAppManager(this).getUser();
    String countries = user.getCountries();
    ArrayList<Country> countriesListe = cm.getCountryByCodes(countries);
    Spinner countrieSpinner = mDialogStart.findViewById(R.id.sp_countries);
    CountriesListAdapter adapter = new CountriesListAdapter(this, R.layout.country_list_item, countriesListe);
    countrieSpinner.setAdapter(adapter);
    country = user.getDefaultCountry();
    int position = adapter.indexOf(country);
    Log.w(TAG, "countries::" + countries + "|| country::" + country + " || position::" + position);
    countrieSpinner.setSelection(position);
    countrieSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
            country = Objects.requireNonNull(adapter.getItem(pos)).getCode();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            country = user.getDefaultCountry();
        }

适配器列表的代码

    public class CountriesListAdapter extends ArrayAdapter<Country> {
private final Context context;
//private final String[] values;
private ArrayList<Country> countries;

public CountriesListAdapter(Context context,int ressource,ArrayList<Country> countries) {
    super(context, ressource, new CountryManager(context).getAllPays());
    this.context = context;
    this.countries = countries;
}

@Override
public int getCount() {
    return countries.size();
}

@Override
public Country getItem(int i) {
    return countries.get(i);
}

public int getPosition(Country item) {
    return countries.indexOf(item);
}

@Override
public long getItemId(int i) {
    return i;
}

public int indexOf( String code) {
    for (int index = 0, count = getCount(); index < count; index++)
    {
        if (getItem(index).getCode().equals(code))  {
            return index;
        }
    }
    return -1;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtViewCountryName);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);

    String prefix = getItem(position).getPrefix();
    String code = getItem(position).getCode();
    String label = getItem(position).getLabel();
    textView.setText(" "+code);

    String buttonID = "drawable/" + code.toLowerCase();
    int resID = context.getResources().getIdentifier(buttonID, "id", context.getPackageName());
    Drawable img = context.getResources().getDrawable( resID);
    textView.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

    return rowView;
}

@Override
public View getDropDownView(int position,  View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtViewCountryName);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);

    String prefix = getItem(position).getPrefix();
    String code = getItem(position).getCode();
    String label = getItem(position).getLabel();
    textView.setText(" "+code);
    textView.setGravity(Gravity.LEFT);

    String buttonID = "drawable/" + code.toLowerCase();
    int resID = context.getResources().getIdentifier(buttonID, "id", context.getPackageName());
    Drawable img = context.getResources().getDrawable( resID);
    textView.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

    return rowView;
}

private String GetCountryZipCode(String ssid){
    Locale loc = new Locale("", ssid);

    return loc.getDisplayCountry().trim();
}
}

我希望从 SQLite 获取每个用户的国家/地区

【问题讨论】:

  • 分享错误logcat。
  • 没有错误,当我调用包含微调器的接口时,应用程序运行但崩溃,我认为这可能是声明问题
  • 没有错误没有人可以帮助你!
  • 如果您的应用程序正在崩溃,那么 logcat 中将有一个来自崩溃的堆栈跟踪,这将有助于确定问题。请查看this Stack Overflow postthis developer page 以获取帮助。
  • 你有一种奇怪的方式来声明drawable id

标签: java android sqlite spinner


【解决方案1】:

问题是适配器是一个空指针。您必须先调用setContentView(&lt;YourView&gt;);,然后再调用setAdapter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2023-03-19
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    相关资源
    最近更新 更多