I am trying to make a basic conversion tool that i will expand on later. But when i take the information from the EditText and covert it to Integer then try to multiply it, my application crashes on the button click.
My Java code is as follows, New member to this site, but have been using it for help for weeks now, So thanks you have all helped out so much already
package com.example.mayconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Feetyards extends Activity{
EditText feet;
Button convertFeet;
int formula;
TextView displayText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.feet_yards);
displayText =(TextView) findViewById(R.id.displayText);
formula = 3;
convertFeet = (Button) findViewById(R.id.buttonFeet);
final EditText feet = (EditText) findViewById(R.id.editTextFeet);
convertFeet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String data = feet.getText().toString();
final int dataint = Integer.parseInt(data);
displayText.setText(dataint + formula);
}
});
}
}
It always helps to post your LogCat errors, but it looks like you have two problems:
You are accidentally creating two variables with the same name
feet:This creates a local
feetvariable (not what you want)Use this instead to initialize your class variable
feet:You need to convert your “formula” back into a String:
When you use
setText(Int), this tried to load a String value stored in your resources, whilesetText(String)displays the String passed to the function.