2013-01-11 41 views
0

將字符串添加到stringArray時遇到問題。它每次崩潰:向stringArray添加字符串android

ArrayList<String> newString, limits; 

    String pruebas;  
    pruebas=e1.getText().toString(); 
    if (pruebas == null || pruebas.equals("")) { 
     Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    else{ 
     limits.add(pruebas); 
    } 

任何幫助,將不勝感激!

三江源

+2

這是字符串數組還是Arraylist ? –

+0

請發佈logcat輸出 –

回答

5

請使用下面的代碼,它會解決你的問題。

代碼添加字符串到ArrayList中

ArrayList<String> limits = new ArrayList<String>(); 

String pruebas=e1.getText().toString(); 

if (pruebas == null || pruebas.equals("")) { 
    Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show(); 
    return; 
}else { 
    limits.add(pruebas); 
} 

代碼添加字符串到字符串數組,這裏10字符串數組和索引的大小是要存儲字符串字符串數組的位置值。

String[] limits = new String[10]; 

String pruebas=e1.getText().toString(); 

if (pruebas == null || pruebas.equals("")) { 
    Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show(); 
    return; 
}else { 
    limits[index] = pruebas; 
} 
+0

如何查看索引的「limits」?例如,'limits [0]'。我試過,它給了我:'表達式的類型必須是一個數組類型,但它解析爲ArrayList ' – Si8

2

您還沒有intialize ArrayList的......所以用這個下面

ArrayList<String> newString, limits; 

    limits = new ArrayList<String>(); 

    newString = new ArrayList<String>(); 

    String pruebas;  
    pruebas=e1.getText().toString(); 
    if (pruebas == null || pruebas.equals("")) { 
     Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    else{ 
     limits.add(pruebas); 
    }