-4

我有 3 个 Spinner 依赖项。但是向我发送切换区域异常以下内容:Invalid IndexOutOfBoundException Location 0 Size is 0

负担始终是 Spinner Region。另外两个以前的依赖。示例:我选择一个地区显示城市。如果我选择一个城市相关的地区显示该城市

第一个时间运行正常,但是当我在 Spinner Region 上选择项目时,微调器 Ciudad 启动异常

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nuevo_cliente);

    posicion=-1;
    txtnCliRut=(EditText) findViewById(R.id.txt_nuRut); 
    txtnCliNombre=(EditText) findViewById(R.id.txt_nuNombre);
    txtnCliDireccion=(EditText) findViewById(R.id.txt_nuDireccion);
    txtnCliTelefono=(EditText) findViewById(R.id.txt_nuTelefono);
    txtnCliRut.setKeyListener(DigitsKeyListener.getInstance("0123456789-kK"));
    txtnCliRut.addTextChangedListener(new InputValidator(txtnCliRut));
    txtnCliTelefono.setInputType(InputType.TYPE_CLASS_PHONE);
    btn_crear=(Button) findViewById(R.id.btnNuCliente);
    btn_cancelar=(Button) findViewById(R.id.btnNuCancela);

    //Declara Spinner
    spRegiones=(Spinner) findViewById(R.id.spRegion);
    spCiudades=(Spinner) findViewById(R.id.spCiudad);
    spComunas=(Spinner) findViewById(R.id.spComuna);
    //Declara Arreglos Objetos
    regiones= new ArrayList<Regiones>();
    ciudades =new ArrayList<Ciudades>();
    comunas =new ArrayList<Comunas>();
    Bundle extras = getIntent().getExtras();
    if (extras != null) 
    {
        posicion= extras.getInt("position");
        ClienteNuevo modCli= new ClienteNuevo();

        modCli=cargaDatos(posicion);
        txtnCliRut.setText(modCli.getRun_nCliente());
        txtnCliNombre.setText(modCli.getNombre_nCliente());
        txtnCliDireccion.setText(modCli.getDireccion_nCLiente());
        txtnCliTelefono.setText(modCli.getTelefono_nCliente());
        btn_crear.setText("Modificar");
    }

    String sFileSrc = null;     
    sFileSrc = "/PDAPedidos/SrcData/regiones.xml";
    oRegion = new ArrayList<Regiones>();
    oParseRegion = new CargaRegionesParserDom(sFileSrc);
    oRegion = oParseRegion.getARegiones();
    for(int i=0;i<oRegion.size();i++)
    {
        regiones.add(oRegion.get(i));
    }

    ArrayAdapter<Regiones> spRegionesAdapter= new ArrayAdapter<Regiones>(this, android.R.layout.simple_spinner_item, regiones);
    spRegionesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spRegiones.setAdapter(spRegionesAdapter);

    spRegiones.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id ) {

            ciudades.clear();
            comunas.clear();
            String sFileSrc = null;     
            sFileSrc = "/PDAPedidos/SrcData/ciudades.xml";
            oCiudad = new ArrayList<Ciudades>();
            oParseCiudad = new CargaCiudadesParserDom(sFileSrc);
            oCiudad = oParseCiudad.getACiudades();
            Log.i("Cargando-Regiones", "Posicion Select REGIOn "+position );
            int idRegion=0;
            idRegion=regiones.get(position).getCodigoRegion();
            for(int i=0;i<oCiudad.size();i++)
            {       
                if(idRegion==oCiudad.get(i).getCodigoRegionCiu())
                {
                    ciudades.add(oCiudad.get(i));
                }
            }

            Log.i("Cargando-Regiones", "Tamaño Arreglo Ciudades"+ ciudades.size());
            ArrayAdapter<Ciudades> spCiudadAdapter= new ArrayAdapter<Ciudades>(getApplicationContext(), android.R.layout.simple_spinner_item,ciudades);
            spCiudadAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spCiudades.setAdapter(spCiudadAdapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
        }
    });

    spCiudades.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id ) 
        {
            comunas.clear();
            String sFileSrc = null;     
            sFileSrc = "/PDAPedidos/SrcData/comunas.xml";
            oComuna = new ArrayList<Comunas>();
            oParseComuna = new CargaComunasParserDom(sFileSrc);
            oComuna = oParseComuna.getAComunas();

            int idCiudad=0;
            Log.i("Cargando-Regiones", "id Ciudad "+position );
            idCiudad=ciudades.get(position).getCodigoCiudad();       
            Log.i("Cargando-Regiones", "id Ciudad "+idCiudad );
            for(int i=0;i<oComuna.size();i++)
            {
                if(idCiudad==oComuna.get(i).getCodigoCiudadCom())
                {
                    comunas.add(oComuna.get(i));
                }
            }

            ArrayAdapter<Comunas> spComunaAdapter= new ArrayAdapter<Comunas>(getApplicationContext(), android.R.layout.simple_spinner_item, comunas);
            spComunaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spComunas.setAdapter(spComunaAdapter); 
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

    final ClienteNuevo oNewCli= new ClienteNuevo();
    btn_crear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            oNewCli.setRun_nCliente(txtnCliRut.getText().toString().trim());
            oNewCli.setNombre_nCliente(txtnCliNombre.getText().toString().trim());
            oNewCli.setDireccion_nCLiente(txtnCliDireccion.getText().toString().trim());
            oNewCli.setCiudad_nCliente(txtnCliCiudad.getText().toString().trim());
            oNewCli.setComuna_nCliente(txtnCliComuna.getText().toString().trim());
            oNewCli.setTelefono_nCliente(txtnCliTelefono.getText().toString().trim());
            if(posicion>=0)
            {
                modificaCli(oNewCli,posicion);
                finish();
            }
            else
            {
                creaNuevoCliente(oNewCli);
                finish();
            }
        }
    });
}

日志猫:

08-08 17:01:11.643: E/AndroidRuntime(18992): Uncaught handler: thread main exiting due to uncaught exception
08-08 17:01:11.673: E/AndroidRuntime(18992): java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
08-08 17:01:11.673: E/AndroidRuntime(18992):    at java.util.ArrayList.get(ArrayList.java:341)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:198)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:909)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ScrollView.onMeasure(ScrollView.java:283)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:453)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.TableLayout.measureVertical(TableLayout.java:465)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.TableLayout.onMeasure(TableLayout.java:428)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.os.Looper.loop(Looper.java:123)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.app.ActivityThread.main(ActivityThread.java:4363)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at java.lang.reflect.Method.invoke(Method.java:521)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

我猜这ciudades是空的(大小== 0),因为:

if(idRegion==oCiudad.get(i).getCodigoRegionCiu())

我看起来您正在尝试比较两种Regiones数据类型。了解在 Java中不能用于==自定义类。你需要使用equals(). 考虑这个简单的例子:

class Foo {
    String bar;

    Foo(String string) {
        this.bar = string;
    }

    @Override
    public boolean equals(Object o) {
        if(o instanceof Foo) // check 1
            return bar.equals(((Foo) o).bar);
        else if(o instanceof String) // check 2
            return bar.equals(o);
        return false; // else
    }
}

现在如果我问:

Foo foo = new Foo("A");
Foo foo2 = new Foo("A");

foo == foo2;              // false, cannot use == operator for custom classes
foo.equals(new Foo("a")); // false, check 1 says "A" is not "a"
foo.equals(foo2);         // true, check 1
foo.equals("A");          // true, check 2
foo.equals(1);            // false, else

另外两个非常有用的重写方法是 toString() 和 hashCode()。

  • toString() 定义了您的类的用户可读表示,我猜您已经这样做了,或者您的 Spinners 中有一些非常丑陋的字符串。

  • hashCode() 被许多内置数据类型使用。例如,如果您想对 Foo 数组进行排序,这是 Java 尝试使用的第一种方法。

以下是 Foo 对这些方法的版本:

@Override
public int hashCode() {
    return bar.hashCode();
}

@Override
public String toString() {
    return bar;
}
于 2012-08-08T21:02:34.607 回答