Crash when changing Input Box for Temperature Conversion


Crash when changing Input Box for Temperature Conversion



I am trying to create a temperature converter without the use of any buttons. I have tried to make it so when I change the text field I am in it will calculate the opposite temperature.
enter image description here



When the app first launches I can type in the Celsius box and get the correct Fahrenheit value. But when I change to the other box, it will just crash when something is entered. In the debug console this is shown:



06-29 17:08:13.007 16809-16819/com.example.mxmol.temperatureconversion I/art: GC statistics: 2 time(s), freed 19853(2010KB) objects, paused 4.271ms total 58.659ms
Background sticky concurrent mark sweep GC freed 5985(237KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 21MB/21MB, paused 7.353ms total 38.658ms at HeapTaskDaemon thread CareAboutPauseTimes 1
06-29 17:08:13.068 16809-16819/com.example.mxmol.temperatureconversion I/art: Background partial concurrent mark sweep GC freed 872(33KB) AllocSpace objects, 0(0B) LOS objects, 15% free, 21MB/25MB, paused 9.031ms total 41.173ms at HeapTaskDaemon thread CareAboutPauseTimes 1



The last line is then repeated with different values, then it crashes. I don't the reason behind this.
Any Ideas? :D


package com.example.mxmol.temperatureconversion;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

TextView calcChangeC;
TextView calcChangeF;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

calcChangeC = (TextView) findViewById(R.id.editTextFahrenheit);
final EditText cToF = (EditText) findViewById(R.id.editTextCelsius);
calcChangeF = (TextView) findViewById(R.id.editTextCelsius);
final EditText ftoC = (EditText) findViewById(R.id.editTextFahrenheit);
cToF.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
cToF.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) {
String cValue = ((EditText) findViewById(R.id.editTextCelsius)).getText().toString();
String fValue = ((EditText) findViewById(R.id.editTextFahrenheit)).getText().toString();
try {
calcChangeC.setText((String.valueOf(Integer.parseInt(cValue) * 1.8 + 32)));
}
catch(NumberFormatException nfe) {
calcChangeC.setText("");
}

catch(Exception e) {
calcChangeC.setText("");
}
}
});
}else{
ftoC.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) {
String fValue = ((EditText) findViewById(R.id.editTextFahrenheit)).getText().toString();
String cValue = ((EditText) findViewById(R.id.editTextCelsius)).getText().toString();
try {
calcChangeF.setText((String.valueOf((Integer.parseInt(fValue) + 32) / 1.8)));
}
catch (NumberFormatException nfe) {
calcChangeF.setText("");
}
catch (Exception e) {
calcChangeF.setText("");
}
}
});
}
}
});

}
}





Possible duplicate of How can I debug this NullPointer Exception?
– Shark
Jun 29 at 15:52





Let's see the actual crash log showing the fatal error where the OS kill the process.
– jdv
Jun 29 at 16:50









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV