由于使用LiveData的索引0无效,自定义ArrayAdapter崩溃

问题描述 投票:0回答:1

我试图使用livedata从房间数据库填充微调器。当我启动应用程序时,微调器正确显示第一个项目。但是,当我单击spiner来选择一个值时,它只会抛出java.lang.IndexOutOfBoundsException:索引0无效,大小为0. Logcat没有说明哪个代码行有问题,但我认为Logcat的下行可以提供一些线索:

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)

问题是我从不使用任何get方法:

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
java android
1个回答
1
投票

ArrayAdapter已拥有自己的一系列物品。

你重写getCount()以返回你自己的数组的大小(这里是>0)但是然后库调用未重写的getItem(),它试图从ArrayAdapter的内部数组(它是空的)返回项目。

解决方案:要么只使用ArrayAdapter的内部数组,要么不扩展ArrayAdapter,而是实现ListAdapter

© www.soinside.com 2019 - 2024. All rights reserved.