我是Android和Java程序的新手,我正在研究我的第一个应用程序,但是遇到了问题。
我有三个EditTexts:FuelAmount
,FuelCost
,CashSpend
以及我想要实现的目标是 - 当填充FA
并且FC
正在归档时,{{ 1}}应该自动填充。
我希望这可以使用所有可能的组合CS
> FA
= FC
,CS
> FA
= CS
等。
所以我尝试了FC
给我所有的addTextChangedListener
。但是,当我测试所有输入方式时,应用程序崩溃的数据
java.lang.StackOverflowError的
关于我放EditTexts
然后FuelCost
或CashSpend
然后FuelAmount
的组合。
CashSpend
有人可以建议我解决方案吗?
提前致谢。
编辑。整个错误
import android.app.DatePickerDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class NewRefuelActivity extends AppCompatActivity {
EditText KmCounter,FuelAmount, FuelPrice,CashSpend,RefuelDate;
Context context = this;
DBHelper dbHelper;
SQLiteDatabase sqLiteDatabase;
Calendar myCalendar = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_refuel);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
KmCounter = (EditText) findViewById(R.id.NewRefuel_KmCounter_Input);
FuelAmount = (EditText) findViewById(R.id.NewRefuel_FuelAmount_Input);
FuelPrice = (EditText) findViewById(R.id.NewRefuel_FuelPrice_Input);
CashSpend = (EditText) findViewById(R.id.NewRefuel_CashSpend_Input);
RefuelDate = (EditText) findViewById(R.id.NewRefuel_Date_Input);
updateLabel_RefuelDate();
RefuelDate.setFocusableInTouchMode(false);
RefuelDate.setFocusable(false);
final DatePickerDialog.OnDateSetListener DTPListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR,year);
myCalendar.set(Calendar.MONTH,monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
updateLabel_RefuelDate();
}
};
RefuelDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(NewRefuelActivity.this,
DTPListener,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
//error
EditText_AutoFill();
}
public void EditText_AutoFill(){
FuelAmount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0){
if (FuelPrice.getText().toString().trim().length() != 0){
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float CS = FA * FC;
CashSpend.setText(CS.toString());
}
else if (CashSpend.getText().toString().trim().length() != 0){
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FC = CS / FA;
FuelAmount.setText(FC.toString());
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
FuelPrice.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0) {
if (FuelAmount.getText().toString().trim().length() != 0) {
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float CS = FA * FC;
CashSpend.setText(CS.toString());
} else if (CashSpend.getText().toString().trim().length() != 0) {
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FA = CS / FC;
FuelAmount.setText(FA.toString());
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
CashSpend.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0) {
if (FuelAmount.getText().toString().trim().length() != 0) {
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float FC = CS / FA;
FuelPrice.setText(FC.toString());
} else if (FuelPrice.getText().toString().trim().length() != 0) {
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float FA = CS / FC;
FuelAmount.setText(FA.toString());
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void updateLabel_RefuelDate(){
String myFormat = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(myFormat, Locale.US);
RefuelDate.setText(simpleDateFormat.format(myCalendar.getTime()));
}
public void addNewRefuel(){
Float km_counter = Float.parseFloat(KmCounter.getText().toString());
Float fuel_amount = Float.parseFloat(FuelAmount.getText().toString());
Float fuel_price = Float.parseFloat(FuelPrice.getText().toString());
Float cash_spend = Float.parseFloat(CashSpend.getText().toString());
String fuel_date = RefuelDate.getText().toString();
dbHelper = new DBHelper(context);
sqLiteDatabase = dbHelper.getWritableDatabase();
dbHelper.addRefuel(fuel_amount, fuel_price, cash_spend, km_counter, fuel_date, sqLiteDatabase);
Toast.makeText(getBaseContext(),"Tankowanie dodane", Toast.LENGTH_SHORT).show();
dbHelper.close();
}
public void clearRefuelEditText(){
//KmCounter.getText().clear();
KmCounter.setText("");
FuelAmount.setText("");
FuelPrice.setText("");
CashSpend.setText("");
RefuelDate.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_new_refuel_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refuel_tab) {
return true;
}
if (id == R.id.action_add_refuel){
addNewRefuel();
clearRefuelEditText();
finish();
//TODO: przetestowac alternatywe finish()
/*
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
*/
return true;
}
case android.R.id.home:
// this takes the user 'back', as if they pressed the left-facing triangle icon on the main android toolbar.
// if this doesn't work as desired, another possibility is to call `finish()` here.
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:0)
你必须记住几件事:
EditText
也可能包含可能导致崩溃的空格。要避免这种情况,而不是让editText.length()
更好地采用editText.getText().toString().trim().length()
。 android:inputType="number"
的数字。