java.lang.NumberFormatException: unable to parse '
java.lang.NumberFormatException: unable to parse '
今日竹石 发表于4年前
java.lang.NumberFormatException: unable to parse '
  • 发表于 4年前
  • 阅读 611
  • 收藏 1
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: java.lang.NumberFormatException: unable to parse '' as integer改如何解决呢?

问题代码:

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>();@Overrideprotected 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);             
        }
    });     }}

解决方法:

方法一:

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;}

方法二:

try

{

totalscore[i] = Integer.parseInt(point[i].getText().toString());

} catch (Exception e)

{

e.printStackTrace();

}



  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 41
博文 228
码字总数 181721
×
今日竹石
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: