从编辑文本中获取信息时的Android编号格式异常

Sya*_*mic -2 android google-maps exception numberformatexception android-edittext

在这里,我从用户那里采取了粗鲁和粗俗,然后我将其转换为整数,这里是日志猫消息的问题:

无法将30.01转换为int!然后我在数据库中输入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();
}}
Run Code Online (Sandbox Code Playgroud)

}

t0m*_*13b 6

首先尝试使用double数据类型.

double dLat=Double.parseDouble(lats);
double dLon=Double.parseDouble(lons);
Run Code Online (Sandbox Code Playgroud)

然后它得到小数点前的值:

int lat = (int)Math.floor(dLat);
int lon = (int)Math.floor(dLon);
Run Code Online (Sandbox Code Playgroud)

或者如果你想 - 你可以先在输入上运行一个正则表达式,以确保它有数字[dot]数字,否则提取数字的输入将失败,但是说,这将取决于布局如何该EditText有它设置为特定的输入类型- android:inputType在XML布局中.