empty EditText caused by wrong view? - android

I'm new to android studio and programming. I create second table to my database and have almost identical onClickListener class first one work perfect but second give me error. EditText .getText().toString() is empty so I can't convert it to dobule. I think this problem may be caused by View but I dont know java that well and I cant find solution.
EDIT: I manage to solve this problem by adding AlertDialog to my second OnClick listener. This is not exactly what I want but it works.

Could you try setting the input type on the edittext as numberDecimal
android:inputType="numberDecimal"
That way, the user will have to input decimal number always if thats what you are expecting the user to enter.
Also, you could do a null check
if(stringWzrost != null && !stringWzrost.trim().equals("")) {
bmiWzrost = Double.valueOf(stringWzrost);
}
Right now I see that its commented out.

Related

Android Appium : Not able enter the text in textbox

//For log in scenario, I just want to enter the userID and password. I tried with different Xpath but still it's not able to identify the box. every-time getting an no such element exception. We are using testNG.
#AndroidFindBy(xpath = "XYZ")
public MobileElement txtUserName;
utils.EnterText(UserNameorMailId,txtUserName);
Different Xpath tried: xyz is placeholder.
//*[#id='username']
//*[#text='Edit Text ; Email Address or Username']
//*[#id='layout_usename']//*[#text='Edit Text ; Email Address or Username']
Error message:
Method threw 'org.openqa.selenium.NoSuchElementException' exception. Cannot evaluate io.appium.java_client.android.AndroidElement$$EnhancerByCGLIB$$34efbdcd.toString()
I am not sure what else I should try and what exactly the issue is...Help is really appreciated ..
What's the exact error you are getting while entering text ? In case you are able to find the element Can you try to use ".//*[#text='Email Address or Username']"
Can you try using AndroidElement instead of MobileElement?
From the docs, I can see that MobileElement is an abstract class which is inherited by AndroidElement class, so using an object of an Abstract class may not work.
MobileElement:
https://appium.github.io/java-client/io/appium/java_client/MobileElement.html
AndroidElement:
https://appium.github.io/java-client/io/appium/java_client/android/AndroidElement.html
Does clicking on the textbox produce the same error? Have you try setValue instead?

Use EditText in a DialogFragment to change TextView in Main Activity?

So I'm working on my first real attempt at an Android app, just a simple scorekeeper for softball games. I've got it tallying scores, outs, etc, and right now it just displays "Home" and "Away." I'd like to give users the chance to enter in actual team names. To this effect, I added a custom AlertDialog that pops up with an EditText, so when you hit "OK" it'll update the Home/Away team name.
The problem is I've been Googling this for most of the week and I've not found a single way to actually do this. I've tried tagging the fragment's layout XML so I can find the EditText, but it always gives me a null reference and crashes the app. I added a TextWatcher that presumably watched the fragment's text, but once changed and hit "OK," nothing happened. Tried adding the TextWatch to the fragment, that crashed I think, it was about two hours ago and I'm exhausted.
Really, I think I need a way to have the fragment find the TextView with the team name and change it to the value of the EditText when I positive click, but I don't know how to do that. Or maybe I've seen it and don't understand it, I've only been doing this about two months. I'd post my code, but I deleted out all the stuff that didn't work because it was taking up most of the screen real estate. Any ideas?
EDIT: So I followed advice below on defining views, that found the value of the EditText presumably, but hitting "OK" just made it set the TextView to a blank value. I think this is because the EditText's contents went away as the dialog was closed. Either that or this is all wrong. View dialogname = getActivity().getLayoutInflater().inflate(R.layout.fragment_home, null);
EditText mEtName = (EditText) dialogname.findViewById(R.id.homeName);
View mainAct = getActivity().getLayoutInflater().inflate(R.layout.activity_softball, null);
TextView oTextView = (TextView) mainAct.findViewById(R.id.teamOne);
newName = mEtName.getText().toString();
oTextView.setText(newName)
I think you are doing wrong at time of defining id for EditText. You need to give it dialog reference.
rather than doing
Edittext edittext = (EditText) findViewById(R.id.editText);
do like
Edittext edittext = (EditText) dialog.findViewById(R.id.editText);
Been through this issue a while ago. I created my own class which extended DialogFragment. When I tried to initialize EditText which was in the dialog, I got null like
mEtName = (EditText)dialog.findViewById(R.id.editText);
So, I initialized it in this way and it worked:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
View dialogName = getActivity().getLayoutInflater().inflate(R.layout.dialog_name, null);
mEtName = (EditText) dialogName.findViewById(R.id.dialog_etxt_name);
}
Where R.layout.dialog_name is my custom dialog layout.

testPreconditions for EditText hint field

I am trying to test that my EditText hint field is not null in junit's
testPreconditions()
There are examples online for asserting that a TextView is not null, but using EditText in the following way
private EditText editTextField;
public void testPreconditions() {
assertNotNull(editTextField);
}
does not work (tests fail).
How do I access the android:hint field of EditText in the assert?
And while this question targets the hint field, answers that also work for similar situations (text of a button, title / elements of a spinner, etc) would be greatly appreciated.
Thanks!
To get hint from EditText use getHint function.
But it looks like your problem is different. Your test fails because your editTextField is null. You should read "Activity Testing" article. Shortly, JUnit is not enough, you need to setup a special Android Test project in order to access UI views of Activity under test.
If you want to check for not null of hint you can use the following code
EditText et = new EditText(this);
if((et.getHint().toString() != null) && (et.getHint().toString().trim().length()>0)){
}

TextView.setText() followed by TextView.invalidate() doesn't update text in view

I have no idea why this doesn't work. The TextView is defined from an tag in the view. The base TextView doesn't have text set and I want to set it in the View on display.
I have tried placing the below in onCreate and onStart but it doesn't seem to work. The last two lines are just for debugging. I can verify that the header does get the text. The thing is, the TextView doesn't actually get updated. Any ideas?
TextView header=(TextView) findViewById(R.id.acheader);
header.setText(R.string.accounts);
header.invalidate();
header=(TextView) findViewById(R.id.acheader);
String blah=(String) header.getText();
Try again removing the text in 4th line
header=(TextView) findViewById(R.id.acheader);
header.invalidate() is not needed.
Instead of String blah = (String) header.getText() try
String blah = heager.getText().toString();
And why are you verifying a "setText()" on text view using code? Why can't you check the
actual output?
The above code might not work the way you are trying to use it, because the redraw of text view is handled by the framework and generally it tries to group item updates (Dirty rectangles to be specific) and update them all at once. It may do it well after your function exits, Try to validate visually, thats the best way.

Getting EditText field from XML

I have an activity in my app, when i call the line
EditText username = (EditText)findViewById(R.id.usernameText);
the app crashes, why is this?
The view has not been inflated, or you are missing
setContentView(R.layout.layout_file);
Yeah either setContentView hasn't been called in onCreate of the activity, or else the view you are trying to get isn't an EditText. What is the Exception you are getting? If its ClassCast then its not an EditText.
Things to check as no more information is provided:
R.id.usernameText is a valid identifier in your layout
Your layout has been set as a content view
Also, if you can edit the post and add the log of what's messing up, it will be more easy to reply.

Categories

Resources