Android java.lang.NumberFormatException:无法解析'';作为整数
我想在名为total[]的TextView中,根据名为point[]的EditText的值设置计算结果。但我能够解析该值并将其放入TextView。它在logcat中给了我一个错误Android java.lang.NumberFormatException:无法解析'';作为整数,android,Android,我想在名为total[]的TextView中,根据名为point[]的EditText的值设置计算结果。但我能够解析该值并将其放入TextView。它在logcat中给了我一个错误 06-27 02:27:01.467: E/AndroidRuntime(275): Caused by: java.lang.NumberFormatException: unable to parse '' as integer 这是logcat中的一个错误。我想从同一版面上的edittext连续向textvi
06-27 02:27:01.467: E/AndroidRuntime(275): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
这是logcat中的一个错误。我想从同一版面上的edittext连续向textview添加整数值
public class player_name extends Activity {
LinearLayout player_name;
TableLayout ply_name;
Bundle b,b1;
List<TextView> allEds = new ArrayList<TextView>();
List<Button> allplus = new ArrayList<Button>();
List<Button> allminus = new ArrayList<Button>();
List<EditText> alledit = new ArrayList<EditText>();
List<TextView> alltotal = new ArrayList<TextView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player_name);
b1 = getIntent().getExtras();
String[] result = b1.getStringArray("playerName");
player_name = (LinearLayout) findViewById(R.id.player_name);
ply_name = new TableLayout(this);
player_name.addView(ply_name);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
TextView[] ed1 = new TextView[result.length+1];
Button[] plus = new Button[result.length+1];
Button[] minus = new Button[result.length+1];
EditText[] point = new EditText[result.length+1];
TextView[] total = new TextView[result.length+1];
TableRow[] TR= new TableRow[result.length+1];
int[] totalscore = null;
String[] temp = null;
Button btnResult = new Button(player_name.this);
btnResult.setText(" click here to get RESULT");
for(int i=0;i<=(result.length-1);i++) {
ed1[i] = new TextView(player_name.this);
plus[i] = new Button(player_name.this);
minus[i] = new Button(player_name.this);
point[i] = new EditText(player_name.this);
total[i] = new TextView(player_name.this);
TR[i] = new TableRow(player_name.this);
allEds.add(ed1[i]);
alltotal.add(total[i]);
alledit.add(point[i]);
allplus.add(plus[i]);
allminus.add(minus[i]);
TR[i].addView(ed1[i]);
TR[i].addView(point[i]);
TR[i].addView(plus[i]);
TR[i].addView(minus[i]);
TR[i].addView(total[i]);
ply_name.addView(TR[i]);
TR[i].setLayoutParams(tableRowParams);
totalscore[i] =Integer.parseInt(point[i].getText().toString());
temp[i] = "" + totalscore[i];
ed1[i].setId(i);
ed1[i].setHeight(50);
ed1[i].setWidth(70);
ed1[i].setText(result[i]);
ed1[i].setTextColor(Color.CYAN);
total[i].setId(i);
total[i].setHeight(50);
total[i].setWidth(70);
total[i].setText(""+0);
total[i].setTextColor(Color.CYAN);
point[i].setId(i);
point[i].setHeight(50);
point[i].setWidth(120);
point[i].setHint(result[i]+"\'s");
point[i].setInputType(InputType.TYPE_CLASS_NUMBER);
point[i].setTextColor(Color.BLACK);
plus[i].setId(i);
plus[i].setHeight(50);
plus[i].setWidth(50);
plus[i].setText("+");
plus[i].setTextColor(Color.BLACK);
minus[i].setId(i);
minus[i].setHeight(50);
minus[i].setWidth(50);
minus[i].setText("-");
minus[i].setTextColor(Color.BLACK);
plus[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
minus[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
player_name.addView(btnResult, lp);
btnResult.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent1 = new Intent(player_name.this,result.class);
startActivity(intent1);
}
});
}
}
public class player\u name扩展活动{
线性布局玩家名称;
tablelayou名称;
b束,b1束;
List alled=new ArrayList();
List allplus=new ArrayList();
List all减号=新的ArrayList();
List alledit=new ArrayList();
List alltotal=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.player_name);
b1=getIntent().getExtras();
String[]result=b1.getStringArray(“playerName”);
player_name=(LinearLayout)findviewbyd(R.id.player_name);
ply_name=新的表格布局(本);
player_name.addView(ply_name);
TableLayout.LayoutParams tableRowParams=新建TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_父级,TableLayout.LayoutParams.MATCH_父级,1.0f);
TextView[]ed1=新的TextView[result.length+1];
按钮[]加上=新按钮[结果.长度+1];
按钮[]减=新按钮[结果.长度+1];
EditText[]点=新的EditText[result.length+1];
TextView[]总计=新的TextView[result.length+1];
TableRow[]TR=新的TableRow[result.length+1];
int[]totalscore=null;
字符串[]temp=null;
Button btnResult=新按钮(玩家名称,此按钮);
setText(“单击此处获取结果”);
对于(int i=0;i您需要检查正在解析的字符串是否为整数。请尝试以下代码:
if (IsInteger(point[i].getText().toString()))
totalscore[i] =Integer.parseInt(point[i].getText().toString());
并添加此功能:
public static boolean IsInteger(String s)
{
if (s == null || s.length() == 0) return false;
for(int i = 0; i < s.length(); i++)
{
if (Character.digit(s.charAt(i), 10) < 0)
return false;
}
return true;
}
公共静态布尔IsInteger(字符串s)
{
如果(s==null | | s.length()==0)返回false;
对于(int i=0;i
我希望这有助于不要解析空字符串;首先检查空白字符串。对于这样一个小错误,代码太多了,伙计,你能删除除获取此执行项的方法之外的所有内容吗?