Witam. Napisałem aplikację na Androida. Działanie samo w sobie jest w porządku, ale gdy nie wpiszę nic do textfieldów a dam submita to aplikacja nieoczekiwanie się zamyka. Jak widać w metodzie onClick próbowałem sprawdzić czy użytkownik uzupełnił wszystkie pola, ale to nie pomaga.
package com.example.kalkulator;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Kalkulator extends Activity implements OnClickListener {
private Button button;
private EditText tSpalanie, tCena, tPrzejechane;
private TextView wynik;
double tempSpalanie, tempCena, tempPrzejechane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tSpalanie = (EditText)findViewById(R.id.tSpalanie);
tCena = (EditText)findViewById(R.id.tCena);
tPrzejechane = (EditText)findViewById(R.id.tPrzejechane);
button = (Button)findViewById(R.id.button);
wynik = (TextView)findViewById(R.id.wynik);
button.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kalkulator, menu);
return true;
}
@Override
public void onClick(View v) {
tempSpalanie = (tSpalanie == null) ? 0.0 : Double.parseDouble(tSpalanie.getText().toString());
tempCena = (tCena == null) ? 0.0 : Double.parseDouble(tCena.getText().toString());
tempPrzejechane = (tPrzejechane == null) ? 0.0 : Double.parseDouble(tPrzejechane.getText().toString());
double cenaZa100 = tempCena*tempSpalanie;
double wynikCaly = Math.round((tempPrzejechane*cenaZa100)/100);
if(tempSpalanie > 0 && tempCena > 0 && tempPrzejechane > 0) {
wynik.setText("Przejedziesz "+tempPrzejechane+"km za "+String.valueOf(wynikCaly)+"zł");
//wynik.setText(String.valueOf(tempSpalanie)+"zł");
}
else{
wynik.setText("Uzupełnij wszystkie pola");
}
}
}