david vidmar
david vidmar

Reputation: 13

Custome ArrayAdapter crashes due to Invalid index 0 with LiveData

I am trying to use livedata to populate spinner from room database. When I start the app spinner shows the first item correctly. But when I click on the spiner to select a value it just throws java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0. Logcat isn't specifit as to what line of code is problematic but I asume the folowing lines of Logcat can provide some clues:

04-07 02:17:46.596 2106-2106/com.example.aci I/getcount: count   0
04-07 02:17:46.596 2106-2106/com.example.aci I/getcount: count   0
04-07 02:17:46.626 2106-2123/com.example.aci D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-07 02:17:46.626 2106-2122/com.example.aci E/SQLiteLog: (283) recovered 100 frames from WAL file /storage/emulated/0/ACI/Baza/ACI.db-wal
04-07 02:17:46.636 2106-2106/com.example.aci D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-07 02:17:46.636 2106-2106/com.example.aci D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
04-07 02:17:46.636 2106-2122/com.example.aci W/FileUtils: Failed to chmod(/storage/emulated/0/ACI/Baza/ACI.db): android.system.ErrnoException: chmod failed: EPERM (Operation not permitted)
04-07 02:17:46.656 2106-2106/com.example.aci I/test: 0
04-07 02:17:46.656 2106-2106/com.example.aci I/test: 1
04-07 02:17:46.706 2106-2123/com.example.aci I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
    OpenGL ES Shader Compiler Version: E031.25.03.06
    Build Date: 06/05/15 Fri
    Local Branch: LA.BF.1.1.1_RB1.05.01.00.042.032
    Remote Branch: 
    Local Patches: 
    Reconstruct Branch: 
04-07 02:17:46.706 2106-2123/com.example.aci I/OpenGLRenderer: Initialized EGL, version 1.4
04-07 02:17:46.736 2106-2123/com.example.aci D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 4096
04-07 02:17:46.736 2106-2123/com.example.aci D/OpenGLRenderer: Enabling debug mode 0
04-07 02:17:46.766 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.786 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.786 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.796 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.796 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.806 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.806 2106-2106/com.example.aci D/TextView: setTypeface with style : 0
04-07 02:17:46.886 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.886 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.886 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.886 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.886 2106-2106/com.example.aci I/nazivi terencev: Seter 95
04-07 02:17:46.916 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.916 2106-2106/com.example.aci I/nazivi terencev: Item 
04-07 02:17:46.916 2106-2106/com.example.aci I/nazivi terencev: Inflate uspel  95
04-07 02:17:46.916 2106-2106/com.example.aci I/nazivi terencev: Nastavljam text  95
04-07 02:17:46.916 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.916 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.916 2106-2106/com.example.aci I/getcount: count   95
04-07 02:17:46.916 2106-2106/com.example.aci I/nazivi terencev: Item 
04-07 02:17:46.916 2106-2106/com.example.aci I/nazivi terencev: Inflate uspel  95
04-07 02:17:46.916 2106-2106/com.example.aci I/nazivi terencev: Nastavljam text  95
04-07 02:17:46.996 2106-2106/com.example.aci I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@292e079c time:731244002
04-07 02:18:05.846 2106-2106/com.example.aci D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
04-07 02:18:05.996 2106-2106/com.example.aci I/getcount: count   95
04-07 02:18:06.006 2106-2106/com.example.aci D/AndroidRuntime: Shutting down VM
04-07 02:18:06.006 2106-2106/com.example.aci E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.aci, PID: 2106
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390)
        at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
        at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getDropDownView(AppCompatSpinner.java:651)
        at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getView(AppCompatSpinner.java:645)
        at android.support.v7.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:567)
        at android.support.v7.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:772)
        at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:798)
        at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:437)
        at android.view.View$PerformClick.run(View.java:21256)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6914)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

The problem is I never use any get methods:

public class CustomArrayAdapter extends ArrayAdapter<String> {

    private List<String> naziviTerencevCatch = new ArrayList<>();
    private LayoutInflater mInflater;
    private int resource;

    public CustomArrayAdapter(@NonNull Context context, int resource, List<String> temp) {
        super(context, resource);
        mInflater = LayoutInflater.from(context);
        this.resource = resource;
        naziviTerencevCatch = temp;
    }

    @Override
    public int getCount() {
        Log.i("getcount","count   "+String.valueOf(naziviTerencevCatch.size()));
        if(naziviTerencevCatch != null)
            return naziviTerencevCatch.size();
        else return 0;

    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Log.i("nazivi terencev","Item ");
        View view = convertView;
        if(view == null){
            view = mInflater.inflate(R.layout.custom_spinner_layout, parent, false);
            Log.i("nazivi terencev","Inflate uspel  "+String.valueOf(naziviTerencevCatch.size()));
        }

        TextView text = (TextView) view.findViewById(R.id.customSpinnerText);



        if(naziviTerencevCatch != null) {
            Log.i("nazivi terencev","Nastavljam text  "+String.valueOf(naziviTerencevCatch.size()));

            //text.setText(naziviTerencevCatch.get(position));
        }
        else{
            text.setText("KRNEKI!");
            Log.i("nazivi terencev","tekst je null  ");
        }
        return view;
    }

    private View createItemView(int position, View convertView, ViewGroup parent){


        return convertView;
    }

    public void setList(List<String> listx){
        naziviTerencevCatch = listx;
        clear();
        notifyDataSetChanged();
        Log.i("nazivi terencev","Seter "+String.valueOf(naziviTerencevCatch.size()));
    }
} ```

What is particularly confusing to me is the fact that it does get populated. But then it throws when i click on it. I know for a fact that when it crashes the size of list in question (naziviTerencevCatch ) is 95. Although the first 2 times the getCount method is caled it is 0. That is due to the fact that data is still being loaded in the background.

Any help to at least point me in the right direction would be greatly appreciated. I am also ataching the entire logcat.

Regards David

Upvotes: 1

Views: 75

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10959

An ArrayAdapter holds already its own array of items.

You override getCount() to return the size of your own array (which is here >0) but then the library calls the not overridden getItem() which tries to return the item from ArrayAdapter's internal array (which is empty).

Solution: Either use only ArrayAdapter's internal array or do not extend ArrayAdapter but implement ListAdapter instead.

Upvotes: 2

Related Questions