Есть кастомный спинер:
public class CustomSpinner extends AppCompatSpinner {
public interface OnSpinnerEventsListener {
void onPopupWindowOpened(Spinner spinner);
void onPopupWindowClosed(Spinner spinner);
}
private OnSpinnerEventsListener mListener;
private boolean mOpenInitiated = false;
public CustomSpinner(Context context) {
super(context);
}
public CustomSpinner(@NonNull Context context, int mode) {
super(context, mode);
}
public CustomSpinner(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSpinner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
public CustomSpinner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, mode, popupTheme);
}
@Override
public boolean performClick() {
mOpenInitiated = true;
if (mListener != null) {
mListener.onPopupWindowOpened(this);
}
return super.performClick();
}
@Override
public void onWindowFocusChanged (boolean hasFocus) {
if (hasBeenOpened() && hasFocus) {
performClosedEvent();
}
}
/**
* Register the listener which will listen for events.
*/
public void setSpinnerEventsListener(
OnSpinnerEventsListener onSpinnerEventsListener) {
mListener = onSpinnerEventsListener;
}
/**
* Propagate the closed Spinner event to the listener from outside if needed.
*/
public void performClosedEvent() {
mOpenInitiated = false;
if (mListener != null) {
mListener.onPopupWindowClosed(this);
}
}
/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}
}
И адаптер к ниму:
public class SpinnerAdapter extends ArrayAdapter<String> {
Context ctx;
String[] contentArray;
Integer[] imageArray;
public SpinnerAdapter(Context context, int resource, String[] objects, Integer[] imageArray) {
super(context, R.layout.drop_list_item, R.id.spinnerTextView, objects);
this.ctx = context;
this.contentArray = objects;
this.imageArray = imageArray;
}
@Override
public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.drop_list_item, parent, false);
TextView textView = (TextView) row.findViewById(R.id.spinnerTextView);
textView.setText(contentArray[position]);
ImageView imageView = (ImageView)row.findViewById(R.id.spinnerImages);
imageView.setImageResource(imageArray[position]);
return row;
}
}
В активности:
String[] textArray = {"Choose Lang", "Russian", "English", "Czech"};
Integer[] imageArray = {R.mipmap.lang_ru_round, R.mipmap.lang_ru_round, R.mipmap.lang_en_round, R.mipmap.lang_cs_round};
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(this, R.layout.drop_list_item, textArray, imageArray);
spinner.setAdapter(spinnerAdapter);
spinner.setSpinnerEventsListener(this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (parentView.getItemAtPosition(position).equals("Choose Lang")) {
//do nothing.
} else {
if (spinner.getSelectedItem().toString().equals("Russian")) {
lang.updateResource("ru");
Intent intent = getIntent();
finish();
startActivity(intent);
} else if (spinner.getSelectedItem().toString().equals("English")) {
lang.updateResource("en");
Intent intent = getIntent();
finish();
startActivity(intent);
} else if (spinner.getSelectedItem().toString().equals("Czech")) {
lang.updateResource("cs");
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
@Override
public void onPopupWindowOpened(Spinner spinner) {
spinner.setBackground(getResources().getDrawable(R.drawable.drop_list_up));
}
@Override
public void onPopupWindowClosed(Spinner spinner) {
spinner.setBackground(getResources().getDrawable(R.drawable.drop_list));
}
При нажатии на спинер выдает ошибку:
android.content.res.Resources$NotFoundException: Resource ID #0x7f08006c
at android.content.res.Resources.getValue(Resources.java:1369)
at android.content.res.MiuiResources.getValue(MiuiResources.java:147)
at android.content.res.Resources.getDrawable(Resources.java:822)
at android.content.res.Resources.getDrawable(Resources.java:789)
at abox.org.aboxopen.BoxesActivity.onPopupWindowOpened(BoxesActivity.java:198)
at abox.org.aboxopen.CustomSpinner.performClick(CustomSpinner.java:48)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Крашит на версси Android 6, выше все работает.
В чем проблема и как ее решить?