我想在EditTextBox(fcon / fuel consumption)中编辑细节。 然而, 当我尝试编辑它时, 它会给我强制关闭错误。
我想知道为什么会这样? 有人可以帮帮我吗?
updateAnddelete.java
public class updateAnddelete extends Activity implements OnClickListener {
// TableLayout tablelayout_Log = null;
private Button deleteButton;
private Button updateButton;
private Button cancelButton;
// Button searchButton = null;
private static EditText dateEdit;
private EditText priceEdit;
private EditText pumpEdit;
private TextView costView;
private EditText odometerEdit;
private TextView fconView;
private TextWatcher textWatcher;
private String priceEditStr ="",pumpEditStr="";
private String odmEditStr = "";
private String lastOdm = "";
private String bundleDate;
private String bundlePrice;
private String bundlePump;
private String bundleCost;
private String bundleOdometer;
private String bundleFcon;
private String dateEditTextValue;
private String priceEditTextValue;
private String pumpEditTextValue;
private String costTextViewValue;
private String odometerEditTextValue;
private String fconTextViewValue;
double result;
double resultCon;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
public boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
costView = (TextView)findViewById(R.id.tcost);
dateEdit = (EditText)findViewById(R.id.date);
priceEdit = (EditText)findViewById(R.id.fuelprice);
pumpEdit = (EditText)findViewById(R.id.fuelpump);
odometerEdit = (EditText)findViewById(R.id.odometer);
fconView = (TextView)findViewById(R.id.fcon);
cancelButton = (Button) findViewById(R.id.cancelBTN);
cancelButton.setOnClickListener(this);
updateButton = (Button) findViewById(R.id.updateBTN);
updateButton.setOnClickListener(this);
deleteButton = (Button) findViewById(R.id.deleteBTN);
deleteButton.setOnClickListener(this);
Bundle takeBundledData = getIntent().getExtras();
// First we need to get the bundle data that pass from the UndergraduateListActivity
bundleDate = takeBundledData.getString("clickedDate");
bundlePrice = takeBundledData.getString("clickedPrice");
bundlePump = takeBundledData.getString("clickedPump");
bundleCost = takeBundledData.getString("clickedCost");
bundleOdometer = takeBundledData.getString("clickedOdometer");
bundleFcon = takeBundledData.getString("clickedFCon");
// Set the values that we extracted from the Bundle in the EditText fields
dateEdit.setText(bundleDate);
priceEdit.setText(bundlePrice);
pumpEdit.setText(bundlePump);
costView.setText(bundleCost);
odometerEdit.setText(bundleOdometer);
fconView.setText(bundleFcon);
dateEdit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
priceEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
});
pumpEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
});
odometerEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
odmEditStr = odometerEdit.getText().toString().trim();
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase(null) && !lastOdm.trim().equalsIgnoreCase(" "))
{
resultCon = Double.parseDouble(odmEditStr) / Double.parseDouble(pumpEditStr);
fconView.setText(" "+resultCon);
}
}
});
}
@Override
public void onClick(View v) {
// We need to update or delete details which is in the EditText fields after user edit the values
// These values are the ContentValues that we are going to use in future
dateEditTextValue = dateEdit.getText().toString();
priceEditTextValue = priceEdit.getText().toString();
pumpEditTextValue = pumpEdit.getText().toString();
costTextViewValue = costView.getText().toString();
odometerEditTextValue = odometerEdit.getText().toString();
fconTextViewValue = fconView.getText().toString();
// It is easy to set values to the POJO class and pass the class instance to the updateUgraduateDetails() method
fuelLogPojo fuelLogPojo = new fuelLogPojo();
fuelLogPojo.setdate(bundleDate);
fuelLogPojo.setprice(bundlePrice);
fuelLogPojo.setpump(bundlePump);
fuelLogPojo.setcost(bundleCost);
fuelLogPojo.setodometer(bundleOdometer);
fuelLogPojo.setfcon(bundleFcon);
if(v.getId() == R.id.cancelBTN){
finish();
}else if(v.getId() == R.id.updateBTN){
updateLog(fuelLogPojo);
}else if(v.getId() == R.id.deleteBTN){
deleteLog(fuelLogPojo);
}
}
private void deleteLog(fuelLogPojo deletefuelLogPojo) {
// TODO Auto-generated method stub
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
String[] whereClauseArgument = new String[1];
whereClauseArgument[0] = deletefuelLogPojo.getdate();
// Only difference between UPDATE and DELETE is
//DELETE does not have ContentValues part
sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_LOG, AndroidOpenDbHelper.KEY_DATE+"=?", whereClauseArgument);
sqliteDatabase.close();
finish();
}
private void updateLog(fuelLogPojo updatefuelLogPojo) {
// TODO Auto-generated method stub
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
// ContentValues class is used to store a set of values
//It is like name-value pairs
// "value" part contains the values that we are going to UPDATE
ContentValues contentValues = new ContentValues();
contentValues.put(AndroidOpenDbHelper.KEY_DATE, dateEditTextValue);
contentValues.put(AndroidOpenDbHelper.KEY_PRICE, priceEditTextValue);
contentValues.put(AndroidOpenDbHelper.KEY_FUEL, pumpEditTextValue);
contentValues.put(AndroidOpenDbHelper.KEY_COST, costTextViewValue);
contentValues.put(AndroidOpenDbHelper.KEY_ODM, odometerEditTextValue);
contentValues.put(AndroidOpenDbHelper.KEY_CON, fconTextViewValue);
// If we are using multiple whereClauseArguments, array size should have to change
String[] whereClauseArgument = new String[1];
whereClauseArgument[0] = updatefuelLogPojo.getdate();
System.out.println("whereClauseArgument[0] is :" + whereClauseArgument[0]);
/**
* This is the normal SQL query for UPDATE
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
*/
sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_LOG, contentValues, AndroidOpenDbHelper.KEY_DATE+"=?", whereClauseArgument);
// For two whereClauseArguments
//sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, BaseColumns._ID+"=? AND name=?", whereClauseArgument);
sqliteDatabase.close();
finish();
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
dateEdit.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}}}
logcat的:
01-24 18:28:46.033: E/AndroidRuntime(8694): FATAL EXCEPTION: main
01-24 18:28:46.033: E/AndroidRuntime(8694): java.lang.NumberFormatException: Invalid double: ""
01-24 18:28:46.033: E/AndroidRuntime(8694): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-24 18:28:46.033: E/AndroidRuntime(8694): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
01-24 18:28:46.033: E/AndroidRuntime(8694): at java.lang.Double.parseDouble(Double.java:295)
01-24 18:28:46.033: E/AndroidRuntime(8694): at com.example.fuellogproject.updateAnddelete$4.afterTextChanged(updateAnddelete.java:210)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.widget.TextView.sendAfterTextChanged(TextView.java:7562)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9383)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:356)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.widget.TextView.doKeyDown(TextView.java:5647)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.widget.TextView.onKeyDown(TextView.java:5459)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.KeyEvent.dispatch(KeyEvent.java:2705)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.View.dispatchKeyEvent(View.java:7263)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1413)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1413)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1413)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1413)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1413)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1413)
01-24 18:28:46.033: E/AndroidRuntime(8694): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2118)
01-24 18:28:46.033: E/AndroidRuntime(8694): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1470)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.app.Activity.dispatchKeyEvent(Activity.java:2426)
01-24 18:28:46.033: E/AndroidRuntime(8694): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2045)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3924)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3858)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3415)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4528)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4506)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3152)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.os.Looper.loop(Looper.java:137)
01-24 18:28:46.033: E/AndroidRuntime(8694): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-24 18:28:46.033: E/AndroidRuntime(8694): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 18:28:46.033: E/AndroidRuntime(8694): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 18:28:46.033: E/AndroidRuntime(8694): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-24 18:28:46.033: E/AndroidRuntime(8694): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-24 18:28:46.033: E/AndroidRuntime(8694): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我想在这里你有错误:
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
原因:
您的priceEditStr
或pumpEditStris
null。
检查null,如..
if(priceEditStr.length()>0 && pumpEditStr.length()>0){
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
}
让我知道它的工作与否。
答案 1 :(得分:1)
您正在获得异常,因为您将变量初始化为String priceEditStr ="",pumpEditStr="";
,因此,当edittext为空时,值不会更改。
所以你需要将变量初始化为
private String priceEditStr ="0.0",pumpEditStr="0.0";
需要替换afterTextChanged()
,如下所示
@Override
public void afterTextChanged(Editable editable) {
if (! android.text.TextUtils.isEmpty(priceEdit.getText())) {
priceEditStr = priceEdit.getText().toString().trim();
}
if (! android.text.TextUtils.isEmpty(pumpEdit.getText())) {
pumpEditStr = pumpEdit.getText().toString().trim();
}
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
答案 2 :(得分:0)
例外是不言自明的:
NumberFormatException:无效的double:“”
这意味着您尝试解析为Double的值不是格式正确的数字。在这种情况下,您尝试将空字符串解析为数字。您应该在解析调用周围放置一个try / catch子句,并使用xml中EditText声明中的以下属性将EditText限制为有效数字:
android:inputType="numberDecimal"