从编辑文本获取信息时出现Android数字格式异常

从编辑文本获取信息时出现Android数字格式异常,android,google-maps,exception,android-edittext,numberformatexception,Android,Google Maps,Exception,Android Edittext,Numberformatexception,在这里,我从用户那里获取latude和longtude,然后将其转换为整数,这里是log cat msg的问题: 无法将30.01转换为整数!然后,我将lat和lon输入数据库中的表中: public class newMission extends Activity implements OnClickListener{ SQLiteDatabase sql; EditText e1,e2,e3,e4; Button b; MaptoDo obj; @Override protec

在这里,我从用户那里获取latude和longtude,然后将其转换为整数,这里是log cat msg的问题:

无法将30.01转换为整数!然后,我将lat和lon输入数据库中的表中:

  public class newMission extends Activity 
  implements OnClickListener{
SQLiteDatabase sql;
EditText e1,e2,e3,e4;
Button b;
MaptoDo obj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Intent in =getIntent();
    setContentView(R.layout.newmission);
    e1=(EditText) findViewById(R.id.edn1);
    e2=(EditText) findViewById(R.id.edn2);
    e3=(EditText) findViewById(R.id.edn3);
    e4=(EditText) findViewById(R.id.edn4);
    b=(Button) findViewById(R.id.btnew);
    b.setOnClickListener(this);
    obj=new MaptoDo();
    sql=openOrCreateDatabase("db",0, null);
    sql.execSQL("CREATE TABLE Mission" +
            "" +
            " (emp integer REFERENCES Employee2 (password)," +
            "oper_n text,cust_n text,lat double," +
            "long double,oper_id integer PRIMARY KEY AUTOINCREMENT)");
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String opN=e1.getText().toString();
    String cuN=e2.getText().toString();
    String lats=e3.getText().toString();
    String lons=e4.getText().toString();
     try {
  int lat=Integer.parseInt(lats);
  int lon=Integer.parseInt(lons);
  sql.execSQL("insert into Mission (oper_n,cust_n,lat,long)values('"+opN+"','"+cuN+"',"+lat+","+lon+");");
  Toast.makeText(this,"Data Inserted",2000).show();
  obj.addMission(lat, lon); 
    //add to ToDo list too 
}catch (NumberFormatException nfe) {
    Toast.makeText(this, "Please Enter Valid Data",2000).show();
}}

}

首先尝试使用
double
数据类型

double dLat=Double.parseDouble(lats);
double dLon=Double.parseDouble(lons);
然后是在小数点之前获取值的问题:

int lat = (int)Math.floor(dLat);
int lon = (int)Math.floor(dLon);

或者,如果您愿意,您可以首先在输入上运行正则表达式,以确保它具有数字[dot]位,否则将无法将输入提取到numeric,但是,这将取决于
EditText
的布局如何为XML布局中的某些输入类型设置-
android:inputType

您无法将字符串“30.01”转换为
int
。尝试将其解析为
float

float lat = Float.parseFloat(lats);
或使用

double lat = Double.parseDouble(lats);

旁白:一定要阅读。是的:在别人回答问题后,你正在改变这个问题。请注意,这些网站不是论坛;我们希望这些问题和答案对未来的游客也有一定的价值。另外,当前标题听起来像“请帮助我调试”-对未来的访问者根本没有用处。@Arjan好的,我知道你想说什么了…谢谢你的建议,我将删除这个问题好吗?呃,不。。。!更糟糕的是,删除人们努力回答的问题。把问题改回原来的状态。(除此之外:)第一个更正:将我的代码更改为:int lat=Integer.parseInt(lats);int lon=Integer.parseInt(lons);加倍dLat=double.parseDouble(lats);double-dLon=double.parseDouble(lons);然后移除MapToDo类的对象任何看到你答案的人都可以投赞成票或反对票…我没有投反对票!我也没有投反对票,但是:当然可以将浮点值(如double)转换为整数。你会失去精度,30.01变成30。但如果那是你想要的,你能做到吗?那么,也许是因为你大胆地说这是不可能的,所以人们投了反对票?另外,
parseDouble
在不自动装箱时提供
double
double
。也就是说:我猜提问者并不是真的想要一个整数;甚至他们的数据库也没有使用整型列。事实上,@Syamic实际上对公认的答案发表了评论:“将我的代码[…]更改为
double dLat=double.parseDouble(lats)
”,就像你上面写的那样。所以,你是对的:根本不需要
int
;我不明白为什么人们会否决你的答案。。。