我有一个android程序,我在其中将EditText字段中的用户浮点值作为输入,并将输入值以JSON格式发送到API。但是,当我单击提交按钮时,应用程序崩溃。该应用程序适用于int值,但当我使用浮点数或小数值时,应用程序崩溃。
错误行:
login.put("NumberOfDays",txtLdays.getText().toString());
logcat的错误:
java.lang.NumberFormatException: Invalid int: "1.2"
代码是
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.json.JSONObject;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class LeaveApp extends Activity implements OnClickListener
{
EditText txtDate ,txtLdays, txtLreasons;
TextView txtDate1,txtEmp, txtBleave;
ImageButton btnCalendar;
Button btnSubmit;
private int mYear, mMonth, mDay;
private Spinner s1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
txtLdays = (EditText) findViewById(R.id.etLdays);
txtLreasons = (EditText) findViewById(R.id.etLreason);
txtDate = (EditText) findViewById(R.id.etSdate);
txtDate1= (TextView) findViewById(R.id.tvHidden);
txtBleave= (TextView) findViewById(R.id.tvL);
btnCalendar = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit = (Button) findViewById(R.id.btnLeave1);
s1=(Spinner)findViewById(R.id.spinnerL);
btnCalendar.setOnClickListener(this);
btnSubmit.setOnClickListener(this);
setCurrentDateOnView();
// empid();
addItemsOnSpinner1();
}
public void onClick(View v)
{
//Calender ImageButton function
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
if (v == btnSubmit)
{
if(isOnline())
{
if(validate1())
{
if(balanceLeaves())
{
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
if (extras != null)
{
String value = extras.getString("new_variable_name");
// Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
}
JSONObject login = new JSONObject();
final String SelectedLeave = s1.getSelectedItem().toString();
try
{
login.put("EmployeeId", strEmployeeID);
login.put("Date",txtDate1.getText().toString());
login.put("TypeLeave",SelectedLeave);
login.put("BalanceLeaves",txtBleave.getText().toString());
login.put("StartDate",txtDate.getText().toString());
login.put("NumberOfDays",txtLdays.getText().toString());
login.put("Reason",txtLreasons.getText().toString());
//
JSONObject finaldata = new JSONObject();
finaldata.put("LeaveRequest", login);
Toast.makeText(getBaseContext(), finaldata.toString(), Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
如果你想要一个浮点值而不是整数,请尝试使用Integer.parseInt(txtLdays.getText()。toString())或Float.parseFloat()。
答案 1 :(得分:0)
您可以通过将输入乘以10来解决此问题。 1.2变为12.记得在API中除以10
答案 2 :(得分:0)
float f = 52.92;
login.put("NumberOfDays", f);
似乎工作。
答案 3 :(得分:0)
试试这个
login.put( “NUMBEROFDAYS”,String.value(txtLdays.getText()));