我有 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)