我的Spinner里面有以下项目:Andrew-15。第一项是名称,第二项是年龄。所以我想将它分成两部分,其中String1存储Andrew和String2存储15.所以这是我的代码
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
TextView myText = (TextView) view;
Toast.makeText(this, "Item selected"+myText.getText(),Toast.LENGTH_SHORT).show();
String text = spinner.getSelectedItem().toString();
String[] parts = text.split("-");
String part1 = parts[0];
String part2 = parts[1];
}
但这让我在Log Cat中出现了一个错误:
FATAL EXCEPTION: main
04-28 00:45:58.907: E/AndroidRuntime(18463): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
04-28 00:45:58.907: E/AndroidRuntime(18463): at com.example.packagename.MainActivity.onItemSelected(MainActivity.java:85)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.widget.AdapterView.access$200(AdapterView.java:49)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.os.Handler.handleCallback(Handler.java:725)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.os.Looper.loop(Looper.java:137)
04-28 00:45:58.907: E/AndroidRuntime(18463): at android.app.ActivityThread.main(ActivityThread.java:5195)
04-28 00:45:58.907: E/AndroidRuntime(18463): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 00:45:58.907: E/AndroidRuntime(18463): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 00:45:58.907: E/AndroidRuntime(18463): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-28 00:45:58.907: E/AndroidRuntime(18463): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-28 00:45:58.907: E/AndroidRuntime(18463): at dalvik.system.NativeStart.main(Native Method)
如何修复此错误?任何帮助将不胜感激。我的文字中有一个值,它是Andrew-15
答案 0 :(得分:0)
它说ArrayIndexOutOfBounds
。所以我的猜测是那个字符串没有“ - ”。因此,拆分后的字符串数组只有一个项目。因此,当您尝试获取第二项时,它会引发异常。
答案 1 :(得分:0)
// try this way,hope this will help you..
**activity1.xml**code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
**Activity** code
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
final String[] spinnerValueArray = new String[]{"Andrew-15","Jack-20","John-25","Smith-18","Martin-15"};
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, spinnerValueArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String name;
String version="";
String [] values = spinnerValueArray[position].split("-");
if(values.length > 1){
name = values[0];
version = values[1];
}else{
name = values[0];
}
Toast.makeText(MyActivity.this,"Name : "+name+" Age : "+version,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}