What is wrong with this code? I've got NumberFormatException. Invalid int: ""
Everything looks correctly but I don't know am I doing my EditTexts correctly. Any help is welcome.
public class Wyslij extends Activity {
EditText et_nazwa;
EditText et_nip;
EditText et_adres;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wyslij);
et_nazwa = (EditText) findViewById(R.id.et_nazwa);
et_nip = (EditText) findViewById(R.id.et_nip);
et_adres = (EditText) findViewById(R.id.et_adres);
ib_wyslij = (ImageButton) findViewById(R.id.ib_wyslij);
String nazwa_firmy = et_nazwa.getText().toString();
String nip_firmowy = et_nip.getText().toString();
int nip_firmy = Integer.valueOf(nip_firmowy);
String adres_firmy = et_adres.getText().toString();
final Zamowienie zam = new Zamowienie();
zam.klient.nazwa = nazwa_firmy;
zam.klient.nip = nip_firmy;
zam.klient.adres = adres_firmy;
String suma_zamowienia = podaj_sume(TowarZamowienie.towary_zamowione);
int suma_zam = Integer.valueOf(suma_zamowienia);
zam.suma=suma_zam;
}
}
public class Wyslij extends Activity {
EditText et_nazwa;
EditText et_nip;
EditText et_adres;
int suma_zam;
int nip_firmy;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wyslij);
et_nazwa = (EditText) findViewById(R.id.et_nazwa);
et_nip = (EditText) findViewById(R.id.et_nip);
et_adres = (EditText) findViewById(R.id.et_adres);
ib_wyslij = (ImageButton) findViewById(R.id.ib_wyslij);
String nazwa_firmy = et_nazwa.getText().toString();
String nip_firmowy = et_nip.getText().toString();
if(nip_firmowy != null && !nip_firmowy.equalsIgnoreCase(""){
nip_firmy = Integer.parseInt(nip_firmowy);
String adres_firmy = et_adres.getText().toString();
final Zamowienie zam = new Zamowienie();
zam.klient.nazwa = nazwa_firmy;
if(nip_firmy != null){
zam.klient.nip = nip_firmy;
}
if(adres_firmy != null){
zam.klient.adres = adres_firmy;
}
}
String suma_zamowienia = podaj_sume(TowarZamowienie.towary_zamowione);
if(suma_zamowienia != null && !suma_zamowienia.equalsIgnoreCase(""){
suma_zam = Integer.parseInt(suma_zamowienia);
zam.suma=suma_zam;}
}
}
I believe there is a white space when you are fetching value from EditText. So before you convert the string value to integer you need to trim the white space like below,
String nip_firmowy = et_nip.getText().toString().trim(); // use trim here
int nip_firmy = Integer.parseInt(nip_firmowy); // use parseInt() method
Related
I've been working on this app that collects information, then sends it in email form. All of my other EditTexts are working with the exception of the very first one, pilot (the hint is name, as in the name of the pilot). I've gone through this thoroughly for multiple hours but I just cant seem to find what is the problem. The only reason I know its null is because when it goes into the email format all it says is null
public class InfoSheet extends AppCompatActivity {
private double VesselUnits;
private EditText pilot, ship, to, from, LOA, MBDTH, CUSD, zone1, zone2, CallSign;
private Spinner agent_spinner;
private Button btnSubmit;
private String date, agent, Spilot, Sship, Sto, Sfrom, Szone1, Szone2, SCallSign, SVesselUnits;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_sheet);
date=getTodaysDate();
addListenerOnButton();
}
public void collectNCalc(){
//grab all of our info
agent_spinner = (Spinner) findViewById(R.id.agent_spinner);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
pilot = (EditText) findViewById(R.id.Pilot);
ship = (EditText) findViewById(R.id.ship);
to = (EditText) findViewById(R.id.to);
from = (EditText) findViewById(R.id.from);
LOA = (EditText) findViewById(R.id.LOA);
MBDTH = (EditText) findViewById(R.id.MBDTH);
CUSD = (EditText) findViewById(R.id.CUSD);
zone1 = (EditText) findViewById(R.id.zone1);
zone2 = (EditText) findViewById(R.id.zone2);
CallSign = (EditText) findViewById(R.id.CallSign);
//convert what we need to int to do equations
String sLOA = LOA.getText().toString();
double intLOA = Integer.valueOf(sLOA);
intLOA = intLOA*3.281;
String sMBDTH = MBDTH.getText().toString();
double intMBDTH = Integer.valueOf(sMBDTH);
intMBDTH = intMBDTH*3.281;
String sCUSD = CUSD.getText().toString();
double intCUSD = Integer.valueOf(sCUSD);
intCUSD = intCUSD*3.281;
VesselUnits = intLOA*intMBDTH*intCUSD;
VesselUnits = VesselUnits/10000;
Spilot=pilot.getText().toString();
Sship=ship.getText().toString();
Sto=to.getText().toString();
Sfrom=from.getText().toString();
Szone1=zone1.getText().toString();
Szone2=zone2.getText().toString();
SCallSign=CallSign.getText().toString();
agent=agent_spinner.getSelectedItem().toString();
//SVesselUnits=String.valueOf(VesselUnits);
}
public void addListenerOnButton() {
final Context context2 = this;
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i2 = new Intent(context2, DigitalSignature.class);
//do the Calc
collectNCalc();
//pass to next activity
i2.putExtra("Pilot",Spilot);
i2.putExtra("ship",Sship);
i2.putExtra("to",Sto);
i2.putExtra("from",Sfrom);
i2.putExtra("zone1",Szone1);
i2.putExtra("zone2",Szone2);
i2.putExtra("callsign",SCallSign);
i2.putExtra("agent",agent);
i2.putExtra("vessleunits",VesselUnits);
i2.putExtra("date",date);
startActivity(i2);
}
});
}
its sent to the next and final activity:
public class DigitalSignature extends AppCompatActivity {
String pilot, ship, to, from, zone1, zone2, CallSign, agent, date;
Toolbar toolbar;
Button btn_get_sign, mClear, mGetSign, mCancel, btn_send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_digital_signature);
Bundle extras = getIntent().getExtras();
if (extras != null) {
pilot = extras.getString("pilot");
ship = extras.getString("ship");
to = extras.getString("to");
from = extras.getString("from");
zone1 = extras.getString("zone1");
zone2 = extras.getString("zone2");
CallSign = extras.getString("callsign");
agent = extras.getString("agent");
vesselUnit = extras.getDouble("vesselunits");
date = extras.getString("date");
}
btn_send.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Uri path = Uri.parse("file://" + file);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_TEXT, pilot+"\n"+ship+"\n"+to+"\n"+from+"\n"+zone1+"\n"+zone2+"\n"+CallSign+"\n"+agent+"\n"+vesselUnit);
// set the type to 'email'
emailIntent.setType("image/png");
String to[] = {"metropilottickets#gmail.com"};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, pilot+"'s Ticket for "+ship);
startActivity(Intent.createChooser(emailIntent , "Send email..."));
}
});
}
I left out a lot of the other code that's irrelevant to my question, but if anyone can point out why I'm getting null you'd be a life saver!
You need to use extras.getString("Pilot");
insteadof extras.getString("pilot");
These are my xml. I have a EditText and a Text View
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/resultado"/>
<EditText
android:id="#+id/a_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Insira a"
android:inputType="number"
android:padding="36dp" />
My java file. I already converted EditText's value to int, I guess. What am I doing wrong?
public class DiagnosticoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);
// Get a value from a editText
EditText editTextA = (EditText) findViewById(R.id.a_edit_text);
TextView resul = (TextView) findViewById(R.id.resultado);
String variableA = editTextA.getText().toString(); //this will get a string
int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
resul.setText(""+a); //Added "" here
}
catch (NumberFormatException e) {
// handle incorrect text entry here
} // a will be 0 if exception occurred
}
}
How can I display entered number with TextView?
I Tried this but it ain't worked...What I should do?
public class DiagnosticoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);
}
public void calcular(View view){
// Get a value from a editText
EditText editTextA = (EditText) findViewById(R.id.a_edit_text);
TextView resul = (TextView) findViewById(R.id.resultado);
String variableA = editTextA.getText().toString(); //this will get a string
int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
resul.setText(a);
}
catch (NumberFormatException e) {
// handle incorrect text entry here
} // a will be 0 if exception occurred
}
}
<Button
android:id="#+id/button_calcular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calcular"
android:layout_marginRight="8dp"
android:onClick="calcular"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/resultado"/>
<EditText
android:id="#+id/a_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Insira a"
android:inputType="number"
android:padding="36dp" />
New java code
public class DiagnosticoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);
}
public void calcular(View view){
// Get a value from a editText
EditText editTextA = (EditText) findViewById(R.id.a_edit_text);
TextView resul = (TextView) findViewById(R.id.resultado);
String variableA = editTextA.getText().toString(); //this will get a string
int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
resul.setText(""+a); //Added "" here
}
catch (NumberFormatException e) {
// handle incorrect text entry here
} // a will be 0 if exception occurred
}
}
Now I want to get a string to a edit view, cast in a double and display into a text view...But my apps crashs!
public class DiagnosticoActivity extends AppCompatActivity {
EditText editTextA;
EditText editTextB;
EditText editTextC;
EditText editTextD;
TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);
//Locate a View
editTextA = (EditText) findViewById(R.id.a_edit_text);
editTextB = (EditText) findViewById(R.id.b);
editTextC = (EditText) findViewById(R.id.c);
editTextD = (EditText) findViewById(R.id.d);
result = (TextView) findViewById(R.id.resultado);
}
public void calcular(View view){
// Get a value from a editText
String variableA = editTextA.getText().toString(); //this will get a string
String variableB = editTextB.getText().toString(); //this will get a string
String variableC = editTextC.getText().toString(); //this will get a string
String variableD = editTextD.getText().toString(); //this will get a string
//Cast a String in a int or double
double a = Double.parseDouble(variableA);
int b = Integer.parseInt(variableB);
int c = Integer.parseInt(variableC);
int d = Integer.parseInt(variableD);
double soma = a;
result.setText("" + soma);
/*double soma = a/b;
String finalresult = new Double(soma).toString();
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(0);
nf.setMaximumFractionDigits(0);
finalresult= nf.format(result);
textView1.setText(finalresult);*/
/*
double soma = a/b;
String stringDouble= Double.toString(soma);
result.setText("" + stringDouble);*/
/*int a = 0;
int b = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
b = Integer.parseInt(variableB);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a);
}
catch (NumberFormatException e) {
a = 0; // handle incorrect text entry here
} // a will be 0 if exception occurred*/
}
}
public class DiagnosticoActivity extends AppCompatActivity {
EditText editTextA;
TextView resul;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);
editTextA = (EditText) findViewById(R.id.a_edit_text);
resul = (TextView) findViewById(R.id.resultado);
}
public void calcular(View view){
// Get a value from a editText
String variableA = editTextA.getText().toString(); //this will get a string
resul.setText(variableA);
int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
}
catch (NumberFormatException e) {
a = 0; // handle incorrect text entry here
} // a will be 0 if exception occurred
}
}
In my Android app I am calculating a double value using values entered into EditTexts and trying to put the answer into a TextView. My code is this:
double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (EditText) findViewById(R.id.gpa);
String gpaString = gpa.getText().toString();
if(gpaString.equals("")){
gpaString = "0";
}
final double gpaDouble = Double.parseDouble(gpaString);
sat = (EditText) findViewById(R.id.sat);
String satString = sat.getText().toString();
if(satString.equals("")){
satString = "0";
}
final int satInt = Integer.parseInt(satString);
act = (EditText) findViewById(R.id.act);
String actString = act.getText().toString();
if(actString.equals("")){
actString = "0";
}
final int actInt = Integer.parseInt(actString);
score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(actInt/36>satInt/2400){
scoreDouble= (0.6*gpaDouble*25)+(0.4*(actInt/36)*100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is "+scoreString);
}else{
scoreDouble = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is "+scoreString);
}
}
});
}
As of now, when the button is pressed the TextView says: "Your score is 0.0." I feel this has something to do with the fact that I set the default values of the EditTexts to 0. Before I did this, I was getting an error stating NumberFormatException: invalid double: "". If this is the problem, how should I fix it. If that is not the problem, what it?
You define the actInt and satInt as final variables, and they will be assigned to a value only once (when the execution of the onCreate method) and the initial value will be zero as at the begining the edittext contain nothing.
To solve this issue:
move he actInt and satInt from local variables to a field variables and remove the final keyword. (I mean define those variables as a private variables inside the class) and assign the values for the variables inside the onclick Method.
public class test extends Activity {
double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
private int satInt;
private int actInt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (EditText) findViewById(R.id.gpa);
sat = (EditText) findViewById(R.id.sat);
act = (EditText) findViewById(R.id.act);
score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String gpaString = gpa.getText().toString();
if (gpaString.equals("")) {
gpaString = "0";
}
double gpaDouble = Double.parseDouble(gpaString);
String satString = sat.getText().toString();
if (satString.equals("")) {
satString = "0";
}
int satInt = Integer.parseInt(satString);
String actString = act.getText().toString();
if (actString.equals("")) {
actString = "0";
}
int actInt = Integer.parseInt(actString);
if (actInt / 36 > satInt / 2400) {
scoreDouble = (0.6 * gpaDouble * 25)
+ (0.4 * (actInt / 36) * 100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is " + scoreString);
} else {
scoreDouble = (0.6 * gpaDouble * 25)
+ (0.4 * (satInt / 2400) * 100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is " + scoreString);
}
}
});
}
}
You are doing everything on your onCreate method. So all your code is called at the start of your application. At this moment, your EditTexts are empty and your code goes to these parts:
if(gpaString.equals("")){
gpaString = "0";
}
if(actString.equals("")){
actString = "0";
}
if (actString.equals("")) {
actString = "0";
}
Which means that your values gpaDouble, actInt and satInt are equals 0.
Then, you are doing the following:
scoreDouble = (0.6*gpaDouble*25)+(0.4*(satInt/36)*100);
and this:
scoreDouble = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
With the 0 values, your scoreDouble value can only be equal to 0.
To fix it, get your EditTexts' texts in the onClick method of your Button.
I'm preety new on android and trying to write an application but I wrote some code and run on android device till here there is no any problem but when I try to launch query window "sorgulama.xml" application stopped my xml file like this
and my java scource file ( java code ) like this
public class Sorgulama extends Activity implements OnClickListener {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnSorgula:
String sorgu = txtSorgu.getText().toString();
Sorgula(sorgu);
break;
case R.id.btnKisaYollar:
KisaYollar();
break;
case R.id.btnTablo:
Tablolar();
break;
}
}
Button btnSorgula, btnKisaYollar, btnTablo;
EditText txtSorgu;
TableLayout tbl;
ResultSetMetaData metaData;
TableRow renkTableRow;
int kolonSayisi = 0, tvId = 0, otoId = 1;
String url,driver,userName ,password;
ArrayList<String> arrayTablolar = new ArrayList<String>();
ArrayList<String> arrayResults = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.sorgulama);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setTitle("MSSQL Uygulaması v1.0");
Kontroller();
Ayarlar();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
}
private void Ayarlar()
{
DB db = new DB(this);
db.open();
Cursor c = db.Query();
String ipAdresi = null, veriTabaniAdi = null, kullaniciAdi = null, sifre = null;
while(c.moveToNext())
{
ipAdresi = c.getString(c.getColumnIndex("IpAdresi"));
veriTabaniAdi = c.getString(c.getColumnIndex("VeriTabaniAdi"));
kullaniciAdi = c.getString(c.getColumnIndex("KullaniciAdi"));
sifre = c.getString(c.getColumnIndex("Sifre"));
}
url = "jdbc:jtds:sqlserver://" + ipAdresi +";databaseName=" +veriTabaniAdi+"";
driver = "net.sourceforge.jtds.jtbc.Driver";
userName = kullaniciAdi;
password = sifre;
db.close();
}
private void Kontroller()
{
btnSorgula = (Button) findViewById(R.id.btnSorgula);
btnKisaYollar = (Button) findViewById(R.id.btnKisaYollar);
btnTablo = (Button) findViewById(R.id.btnTablo);
txtSorgu = (EditText) findViewById(R.id.txtSorgu);
tbl = (TableLayout) findViewById(R.id.tblSonuc);
btnSorgula.setOnClickListener(this);
btnKisaYollar.setOnClickListener(this);
btnTablo.setOnClickListener(this);
}
private void KisaYollar(){
final CharSequence cs[];
cs = new String[5];
cs[0] = "select";
cs[1] = "*";
cs[2] = "from";
cs[3] = "where";
cs[4] = "and";
cs[5] = "or";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Kısa Yollar").setIcon(R.drawable.logo).setItems(cs, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String sorgu = txtSorgu.getText().toString();
txtSorgu.setText(sorgu+cs[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void Tablolar(){
}
private void Sorgula(String sorgu)
{
}
}
Can anyone help me about this issue pls?
you missed super.onCreate(savedInstanceState). Also
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
must be called before setContentView
I made an Android app wherein I receive text and numeric (decimal) values from user in 5 different activities then store them in my sharedpreferences. I am able to restore all values using editor(); in all previous 5 activities.
Now
In final calculation activity I'm restoring all user input (text and numbers) and storing them in string and double variables. performing calculations in below posted code activity;
Problem
I am unable to view results in textview using getText.
Is there anything missing?
public class Xcalculateforall extends Activity {
Button qcbut, coatsolbut;
TextView TVqcbut, TVcoatsolbut, TVbrand;
double tdrum, rqctotal;
String a;
public static String FILE1 = "MyPrefsFile";
SharedPreferences abcPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.resultmain);
qcbut = (Button) findViewById(R.id.bQCS);
coatsolbut = (Button) findViewById(R.id.bCSP);
TVqcbut = (TextView) findViewById(R.id.tvQCS);
TVbrand = (TextView) findViewById(R.id.tvBrand);
TVcoatsolbut = (TextView) findViewById(R.id.tvCSP);
abcPref = this.getSharedPreferences(FILE1, 0);
a = abcPref.getString("tA", ""); //receives text value and store in 'a' String
double k = Integer.parseInt(abcPref.getString("tsK", ""));
double l = Integer.parseInt(abcPref.getString("tsL", ""));
double m = Integer.parseInt(abcPref.getString("tsM", ""));
double n = Integer.parseInt(abcPref.getString("tsN", ""));
double o = Integer.parseInt(abcPref.getString("tsO", ""));
double p = Integer.parseInt(abcPref.getString("tsP", ""));
tdrum = 20 / m;
double samv = (tdrum / k) / l;
double samv2 = (Math.sqrt(samv));
double tsample = ((samv2 + 1) * k) * l;
double rmicro = tsample * n;
double rchem = tsample * o;
double rother = tsample * p;
double rinqc = rmicro + rchem + rother;
rqctotal = rinqc - 2;
qcbut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TVqcbut.setText(rqctotal + "Days");
TVbrand.setText("" + a);
// Intent results = new Intent("com.traviss.calculate.RESULT");
// startActivity(results);
}
});
}
}
Updating query --- previous activity where i take user input
public class G2J extends Activity {
Button two2five, save2;
EditText edtG, edtH, edtI, edtJ, edtK;
int tG, tH, tI, tJ, tK;
String tsG, tsH, tsI, tsJ, tsK;
public static String FileP2 = "MyPrefsFile";
SharedPreferences abcPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.g2j);
two2five = (Button) findViewById(R.id.btp2);
edtG = (EditText) findViewById(R.id.etG);
edtH = (EditText) findViewById(R.id.etH);
edtI = (EditText) findViewById(R.id.etI);
edtJ = (EditText) findViewById(R.id.etJ);
edtK = (EditText) findViewById(R.id.etK);
abcPref = getSharedPreferences(FileP2, 0);
edtG.setText(abcPref.getString("tsG", ""));
edtH.setText(abcPref.getString("tsH", ""));
edtI.setText(abcPref.getString("tsI", ""));
edtJ.setText(abcPref.getString("tsJ", ""));
edtK.setText(abcPref.getString("tsK", ""));
two2five.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((!edtG.getText().toString().equals(""))
&& (!edtH.getText().toString().equals(""))
&& (!edtI.getText().toString().equals(""))
&& (!edtJ.getText().toString().equals(""))
&& (!edtK.getText().toString().equals(""))) {
abcPref = G2J.this.getSharedPreferences(FileP2, 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tsG", edtG.getText().toString());
editor.putString("tsH", edtH.getText().toString());
editor.putString("tsI", edtI.getText().toString());
editor.putString("tsJ", edtJ.getText().toString());
editor.putString("tsK", edtK.getText().toString());
editor.commit();
Toast message = Toast.makeText(G2J.this, "Values are saved", 2000);
message.setGravity(Gravity.BOTTOM, 0, 0);
message.show();
Intent openl2p = new Intent("com.traviss.calculate.L2P");
startActivity(openl2p);
}
else {
Toast failz = Toast.makeText(G2J.this,
"Values are not Entered", 2000);
failz.setGravity(Gravity.BOTTOM, 0, 0);
failz.show();
}
};
});
}
}
check your string variables and xml layout
Try -
int k = Integer.parseInt(abcPref.getString("tsK", ""));
You wont get decimal values using Integer.parseInt(String)
Do this
double k = Double.parseDouble(abcPref.getString("tsK", ""));
a = String.valueOf(abcPref.getString("tA", ""));