android数字格式异常无效int“”

时间:2014-05-02 05:27:08

标签: android

我正在制作一个应用程序,我正在使用解析代码。转换值。,但我得到数字格式异常。我的代码是

public void checkPointsResponse(String val)
{

    int points;
    progressDialog.dismiss();
     points = Integer.parseInt(val);
    if(points > 0)
    {
        Intent i=new Intent(Contact_ListActivity.this,Account.class);
        startActivity(i);
    }
    else
    {
        Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
        startActivity(i);
    }
}

这里是logcat图片 enter image description here

提前感谢。

4 个答案:

答案 0 :(得分:3)

Convert之前String val Integer检查val为空或不是

if(!val.trim().equals("")){
//Convert val to Integer and do your job
}

您得到Number Format Exception因为您的val为null并且您将null值转换为Integer。

答案 1 :(得分:1)

尝试以下代码: -

显示错误,因为您的val为空且未转换为整数。

public void checkPointsResponse(String val)
                {

                    int points;
                    progressDialog.dismiss();
                    if(!val.trim().equals(""))// check if val empty 
                    {
                         points = Integer.parseInt(val.trim());
                        if(points > 0)
                        {
                            Intent i=new Intent(Contact_ListActivity.this,Account.class);
                            startActivity(i);
                        }
                        else
                        {
                            Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
                            startActivity(i);
                        }
                    }
                }

答案 2 :(得分:1)

试试这个:

public void checkPointsResponse(String val)
{

int points;
progressDialog.dismiss();
if(!val.isEmpty()) {
points = Integer.parseInt(val);
if(points > 0)
  {
    Intent i=new Intent(Contact_ListActivity.this,Account.class);
    startActivity(i);
  }
else
  {
    Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
    startActivity(i);
  }
 }
}

答案 3 :(得分:1)

// try this way,hope this will help you...

public void checkPointsResponse(String val)
    {

        int points;
        progressDialog.dismiss();
        if(val.trim().length()>0){
            points = Integer.parseInt(val);
            if(points > 0)
            {
                Intent i=new Intent(Contact_ListActivity.this,Account.class);
                startActivity(i);
            }
            else
            {
                Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
                startActivity(i);
            }
        }else{
            Toast.makeText(getApplicationContext(), "Value not found", Toast.LENGTH_SHORT).show();
        }
    }