java.lang.NumberFormatException: Invalid int: "" error [duplicate] - java

This question already has an answer here:
Android discount app is not running [closed]
(1 answer)
Closed 8 years ago.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ayak1=(EditText) findViewById(R.id.editText1);
say1 = Integer.parseInt(ayak1.getText().toString());
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==button1){
int sonuc=(say1)*(6)/(10);
String total = String.valueOf(sonuc);
fiyat.setText(total);
}
this is the code,can you please help?

Your ayak1.getText().toString() String is empty. Integer.parseInt() would give you NumberFormatException if you pass to it an empty String (or any String that can't be parsed as an int).

Related

my code in android studio (i think it is in (toString)) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have a problem in my code in android studio, I created it to say "Hello" when the person "abc" writes but that didn't work . can u plz help me. here is my codes
final Button butt=(Button)findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText frag =(EditText)findViewById(R.id.frag);
final TextView hello=(TextView)findViewById(R.id.hello);
String verb =frag.getText().toString();
if (verb=="abc"){
hello.setText("Hello");
Because the condition in if block returns false
Use this code instead.
final Button butt = (Button) findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText frag = (EditText) findViewById(R.id.frag);
final TextView hello = (TextView) findViewById(R.id.hello);
String verb = frag.getText().toString();
if ("abc".equals(verb)) {
hello.setText("Hello");
}
}
}
When comparing string, always use .equals method
Example:
String str1 = "yourstring1";
String str2 = "yourstring2";
if(str1.equals(str2))//return false
{...}

how set variable text in text view [duplicate]

This question already has answers here:
how to resolve java.lang.string cannot be cast to java.lang.integer android with xml parser
(2 answers)
Converting a string to an integer on Android
(14 answers)
Closed 5 years ago.
In the text view, I'll put a variable value of the string as text.
The error I'm facing is:
FATAL EXCEPTION: main Process: app.com.application, PID: 14336 Java. Lang. ClassCastException: Java. Lang. String cannot cast to Java. Lang. Integer atapp.com.application.activity.PayActivity$1.onCheckedChanged(PayActivity.java:41)
Also, this is my code for this part:
public class PayActivity extends AppCompatActivity {
Button button;
RadioGroup radioGroup;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
button = findViewById(R.id.btnPay);
radioGroup = findViewById(R.id.radioPay);
textView = findViewById(R.id.pay);
final String[] TextShow ={
"مبلغ این بسته 2500 تومان است",
"مبلغ این بسته 7000 تومان است",
"مبلغ این بسته 10000 تومان است",
"مبلغ این بسته 20000 تومان است"
};
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedId);
final int ArticleTag = (int) radioButton.getTag();
textView.setText(TextShow[ArticleTag]);
Toast.makeText(PayActivity.this,TextShow[ArticleTag] , Toast.LENGTH_SHORT).show();
}
});
}
}
Also, tags include "0" ,"1", ,"2" and "3".
Can you guide me where is my problem?
Thanks
You can't cast a string containing an integer value to an integer value:
(int) radioButton.getTag()
You have to explicitly convert to a string (since getTag() returns an Object), then parse:
Integer.parseInt(radioButton.getTag().toString())

Causing Null pointer Exception [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Causing error when setText a value in edittext. Id Given is Correct ,if i give set Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
Code Given Below here i cant setText Otp Code "otpcode.setText("12345");" in oncreate it works perfectely.
when i give it in the method"recivedSms".it didn't work.
public class Change_Password_Activity extends AppCompatActivity {
EditText user_name,pass_wd;
public EditText otpcode;
private Button btn_submit;
private String username,otp,password;
private ProgressDialog prgDialog;
private Typeface typeface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change__password_);
typeface = GlobalVariables.getTypeface(Change_Password_Activity.this);
prgDialog = new ProgressDialog(this);
// Set Progress Dialog Text
prgDialog.setMessage("Please wait...");
// Set Cancelable as False
prgDialog.setCancelable(false);
otpcode = (EditText)findViewById(R.id.otpedittext);
user_name = (EditText) findViewById(R.id.edittext_ch_user);
pass_wd = (EditText) findViewById(R.id.edittext_ch_passwd);
btn_submit = (Button) findViewById(R.id.button_changepswd);
otpcode.setTypeface(typeface);
user_name.setTypeface(typeface);
pass_wd.setTypeface(typeface);
btn_submit.setTypeface(typeface);
}
public void recivedSms(String message)
{
try
{
int smsnumbr= Integer.parseInt(message);
otpcode.setText(smsnumbr);
}
catch (Exception e)
{
Log.e("error", String.valueOf(e));
}
if you are trying to set smsnumbr to edittext then it will give null pointer exception as in setText(Integer) android tries to find a resource from R.java file with given integer as id. to achieve what you want you should use String.valueOf(..) instead.
int smsnumbr= Integer.parseInt(message);
otpcode.setText(String.valueOf(smsnumb));

java.lang.NumberFormatException: Invalid double: "" Edit Text [duplicate]

This question already has answers here:
java.lang.numberformatexception: invalid double: " "
(6 answers)
Closed 6 years ago.
I have 6 edit text fields, and I'm trying to input them into a sqlite database and I'm trying to parse 5 of them into doubles, but it throws that error and I can not figure out what to do.
I've tried to use valueOf(String) and parsing, but neither seem to work.
I've used Long's and Integers as well, and have come to the same issue as well
Here's the code:
public class EditClass extends AppCompatActivity {
EditText name, exam, quiz, assignment, participation, lab;
String iName, sExam, sQuiz, sAss, sPart, sLab ;
double iExam, iQuiz, iLab, iAss, iPart;
FloatingActionButton fab;
Database database;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_class);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
name = (EditText) findViewById(R.id.class_name);
exam = (EditText) findViewById(R.id.exam_weight);
quiz = (EditText) findViewById(R.id.quiz_weight);
assignment = (EditText) findViewById(R.id.assignment_weight);
participation = (EditText) findViewById(R.id.participation_weight);
lab = (EditText) findViewById(R.id.lab_weight);
iName = name.getText().toString();
sExam = exam.getText().toString();
sQuiz = quiz.getText().toString();
sAss = assignment.getText().toString();
sPart = participation.getText().toString();
ImageView create = new ImageView(this);
create.setImageDrawable(getResources().getDrawable(R.drawable.checkmark));
fab = new FloatingActionButton.Builder(this).setContentView(create).build();
fab.setBackground(getResources().getDrawable(R.drawable.button_action_lightblue_ztek));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
addData();
}
public void addData(){
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
/**
* Errors with parsing into integer form, comes up with NumberFormatException
*/
database.insertData(iName,
iExam = Double.valueOf(sExam),
iQuiz = Double.valueOf(sQuiz),
iLab = Double.valueOf(sLab),
iAss = Double.valueOf(sAss),
iPart = Double.valueOf(sPart));
Toast.makeText(EditClass.this, "Data was Successfully Inserted", Toast.LENGTH_SHORT).show();
}catch (Exception e){
String error= e.toString();
Toast.makeText(EditClass.this, "Data was Not Inserted", Toast.LENGTH_SHORT).show();
System.out.println(error);
}
}
});
}}
I've now ended up changing the addData method to this, but it comes up as null now, for both the string and doubles.
Here's the method:
public void addData(){
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
/**
* Errors with parsing into integer form, comes up with NumberFormatException
*/
database.insertData(name.getText().toString(),
Double.parseDouble(exam.getText().toString()),
Double.parseDouble(quiz.getText().toString()),
Double.parseDouble(lab.getText().toString()),
Double.parseDouble(assignment.getText().toString()),
Double.parseDouble(participation.getText().toString()));
Toast.makeText(EditClass.this, "Data was Successfully Inserted", Toast.LENGTH_SHORT).show();
}catch (Exception e){
String error= e.toString();
Toast.makeText(EditClass.this, "Data was Not Inserted", Toast.LENGTH_SHORT).show();
System.out.println(error);
}
}
});
}
onCreate() only runs once and is currently the only place you call getText(). Therefore, the values in iName, sExam, etc are all the default values you have for those fields - not the current values when your FloatingActionButton is clicked.
Instead, move the iName = name.getText().toString(); etc lines into your OnClickListener to get the current values when the button is clicked.
Also note that you cannot use Double.valueOf() on an empty field (that's the NumberFormatException you are getting) - consider wrapping each call in TextUtils.isEmpty() to check to see if there is any value at all.

Solution to equation won't display? (no errors being reported on my end)

So I managed to take 4 different activities involving 4 variables and wrote them in this kind of format:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step1);
Button c = (Button) this.findViewById(R.id.continue1);
final EditText input = (EditText) findViewById(R.id.enterscore);
input.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String score1 = input.getText().toString();
Double.parseDouble(score1);
{ Intent myintent = (new Intent(step1.this,step2.class));
myintent.putExtra("SCORE1",score1);
startActivity(myintent);
}
On my final activity the one that's suppose to display the solution I have coded the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
}
public void solutionf(View v) {
Bundle extras = getIntent().getExtras();
if(extras!=null){
double score1 = extras.getDouble("SCORE1");
double score2 = extras.getDouble("SCOREF");
double l2 = extras.getDouble("L2");
double l3= extras.getDouble("L3");
double solution= (((score2-score1)/l2)*l3);
String resultString = "Result:" +solution;
TextView resultText= (TextView) findViewById(R.id.solution);
resultText.setText(resultstring);
}
}
}
Why won't the final solution display? I've checked over to make sure everything matches up in terms of the doubles that are set in the activities. Could it be I didn't properly bring the variables over from the previous activities through error on my code?
Any help to getting the solution to show would be appreciated!
Unless a part of your code is missing, solution() is never called.
Add a call to this function in onCreate of your second activity, it should help.

Categories

Resources