NVIDIA SHIELD Tablet Gets Android 5.0.1, OTA Available

The NVIDIA SHIELD Tablet is the latest Android-powered device released by famed GPU … more

FXP Team Delivers Lollipop to Various Xperia Devices!

From all of us here at XDA-Developers, we’d like to wish you Happy Holidays! If … more

Turn Your Camera Into a Live Puzzle Game

The classic sliding puzzle game hasnt changed much, if at all, over the years. Dating all the way … more

Samsung Begins Production of 8 Gb RAM Chips

With the release of Android 5.0, our favorite mobile operating system has become 64-bit ready. … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] Need help with a program.

OP SurajKamble

31st August 2014, 04:23 PM   |  #1  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Aug 2014
public void click(View view) {
String one = "one";
EditText et = (EditText)findViewById(R.id.editText1);
String entered_text = et.getText().toString();

if(et.getText().toString() == one){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Correct!");
}
else{
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(one+entered_text); }
}
This is a code snippet extracted from my program, i didn't post the whole program because it wasn't necessary, as the program runs fine without any runtime exceptions.
So, the program when executed on eclipse doesn't show any errors and runs fine, but when run the "if" condition "et.getText().toString() == "one"" always returns false even when the "entered_text" is "one" i.e.; it never prints "correct!" and the code always prints "one+entered_text" that is the statement in the else clause. And the interesting thing is, if you enter "one" the output will be "oneone", that is the else statement.
Please help me where i went wrong.
Thanks in advance.
31st August 2014, 04:48 PM   |  #2  
Junior Member
Flag Wrocław
Thanks Meter: 1
 
14 posts
Join Date:Joined: Jun 2014
Donate to Me
More
You're passing view argument in function so try initialize edit text with (EditText)view.findViewById(R.id.edittext);
31st August 2014, 04:57 PM   |  #3  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Aug 2014
Quote:
Originally Posted by panwrona

You're passing view argument in function so try initialize edit text with (EditText)view.findViewById(R.id.edittext);

Thanks for the reply.
I did what you said and got a runtime exception.
31st August 2014, 05:01 PM   |  #4  
Junior Member
Flag Wrocław
Thanks Meter: 1
 
14 posts
Join Date:Joined: Jun 2014
Donate to Me
More
You're running it in fragment or activity?
31st August 2014, 05:02 PM   |  #5  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Aug 2014
Quote:
Originally Posted by panwrona

You're running it in fragment or activity?

Activity.
31st August 2014, 06:08 PM   |  #6  
Junior Member
Flag Wrocław
Thanks Meter: 1
 
14 posts
Join Date:Joined: Jun 2014
Donate to Me
More
Are you initializing it in oncreate or somewhere else?
31st August 2014, 06:25 PM   |  #7  
arpitkh96's Avatar
Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
That's easy. Instead of == use text.equals(one)
String are not compared by mathematical signs

Sent from my XT1033 using XDA Premium 4 mobile app

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes