0

My android application keeps crashing when I call the dialog containing this spinner, I'm sure my XML is ok

I tried to spot the problem by making the code as a comment and it turns that the problem is with the setAdapter line

Please if anyone can help, it will be so cool.

    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();
        }

code for the adapter list

    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();
}
}

I expect to get countries for each user from SQLite

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rami
  • 29
  • 1
  • 5
  • 1
    Share error logcat. – Hemant Parmar Apr 12 '19 at 09:11
  • there is no err, the app runs but crush when i call the interface that contains spinner, i think it maybe a declaration problem – Rami Apr 12 '19 at 09:33
  • without error no one can help you! – Hemant Parmar Apr 12 '19 at 09:34
  • If your app is crashing, then there will be a stack trace from the crash in the logcat that will help to determine the issue. Have a look at [this Stack Overflow post](https://stackoverflow.com/questions/23353173) and [this developer page](https://developer.android.com/studio/debug/am-logcat) for help in finding that. – Mike M. Apr 12 '19 at 09:40
  • You have a strange way for declaring drawable id – Vodet Apr 12 '19 at 09:52
  • Check the stacktrace or logcat when app crashes. It show the specific file and line number where exactly app gets crashed. – Rajesh Shetty Apr 12 '19 at 10:54
  • the app crashes in this line countrieSpinner.setAdapter(adapter); – Rami Apr 12 '19 at 11:09
  • You've mentioned that already. How do you know that, exactly? Are you stepping through with the debugger? Have you found the stack trace? Or are you just saying that it only crashes when you include that line? – Mike M. Apr 12 '19 at 11:39
  • it only crashes when i include that line – Rami Apr 12 '19 at 12:17
  • Without the stack trace, we can't be certain of the current issue, but, as Vodet alludes to above, your `getIdentifier()` call is wrong, which will lead to an error. Assuming that `code.toLowerCase()` is a valid name for a drawable that actually exists in your resources (no file extension), it would be instead `getIdentifier(code.toLowerCase(), "drawable", context.getPackageName())`. – Mike M. Apr 12 '19 at 12:21

1 Answers1

0

The problem is that the adapter is a null pointer. You have to call setContentView(<YourView>); at the beginning before calling setAdapter.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
B-ROK
  • 1