I try to make a quiz app. I need to know which radiobutton has been chosen and check that with the real correct answer. I send the trueResult(score var.) to another ResultActivity class if it is need to be known.
if(((RadioButton) grp_options.getChildAt(grp_options.getCheckedRadioButtonId())).getText().equals(rightAnswers.get(i))) // Error at this expression
{
trueResult++;
}
When i change the part below
grp_options.getChildAt(grp_options.getCheckedRadioButtonId()))
to this
grp_options.getChildAt(3))
it properly compares the 4th option with the real answer. But i need to learn which rb user checked. So the problem is here :
grp_options.getCheckedRadioButtonId()
After all, the app gives me NPE. Here is the log. If there is a better way to do this job or a point i am missing, i would love to learn.
getCheckedRadioButtonId returns the view id, ie. the android:id.
If you have set the ids in the layout, you should be able to retrieve the element with findViewById:
grp_options.findViewById(grp_options.getCheckedRadioButtonId())
I do not know for sure, but you might try this:
Instead of this:
if(((RadioButton) grp_options.getChildAt(grp_options.getCheckedRadioButtonId())).getText().equals(rightAnswers.get(i)))
try this:
if(((RadioButton) findViewById(grp_options.getCheckedRadioButtonId())).getText().toString().equals(rightAnswers.get(i)))
I think your problem might be that you forgot to use toString() in your solution, maybe this works fine too:
if(((RadioButton) grp_options.getChildAt(grp_options.getCheckedRadioButtonId())).getText().toString().equals(rightAnswers.get(i)))
Related
I really hope this question has not been answered everywhere else already but every search seems to focus on listeners and other uses of a button array but i want to also use it for formatting all buttons at the same time (activate, deactivate etc)
So here is what I have tried;
val buttons = arrayOf(btn1,btn2,btn3,btn4)
This will work, BUT will only change a single button
buttons[0].isEnabled=true; //
Then this is the bit that I am struggling with;
buttons[0..buttons.size].isEnabled=true;
The response is basically that it expects a single number and not a range.
I also tried;
buttons[].isEnabled=true;
The response is that it requires an index
I also tried
buttons.isEnabled=true;
This of course will not resolve properly
My key question really is can I apply formatting/state changes to all using an array or will I always have to do it for each button in turn?
I think it would be possible to create a loop but that isn't the route I wanted to follow here if there is an alternative
Don't think there is a way mate. You gotta loop and regardless of what syntatic sugar a language has in the end its still a for loop.
You could do:
buttons.forEach {
it.isEnabled = true
}
I'm writing an Android program I put a ImageView on screen and then remove it but another part of the code is still trying to detect the ImageView after it has been removed. How do I write an if statement that will allow me to detect if the imageView has been removed? I've tried a few things but get no result
if (arrowObj000.get(6).findViewWithTag(arrowObj000.get(6))!=null) {
}
arrowObj00 is a list object/array holding refrences to the ImageView this isn't the problem though
Problem is I need to write an if statement that detects if the ImageView is currently attached.
Walk up the chain of parents, comparing each to the root view of the activity/fragment. If you hit the root, you're attached. If you find a null first, you aren't.
Although a better way to handle this would probably to store the state information somewhere and not use your view as state.
After thoroughly looking through the code hints I found the answer
if (arrowObj000.get(6).isAttachedToWindow()) {
}
I have this simple Espresso interaction:
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.check(matches(allOf(isDisplayed(), isEnabled())))
.perform(typeText("1"));
The check(matches(allOf(isDisplayed(), isEnabled()))) passes as expected, but the following perform(typeText("1")) does not. I cannot figure out why, for the life of me.
So, I can't believe I'm asking this, but how in the name of Android do I use Espresso to type text into my EditText whose ID is R.id.editTextTextWidget?
I fixed the problem by splitting the check(...) call and perform(...) call:
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.check(matches(allOf(isDisplayed(), isEnabled())));
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.perform(typeText("1"));
For some reason this works, and the original doesn't. #GooglePlz
I need help with using an if statement in java, here is my code :
if(ans==1)
{
txtans.setText("This is a Prime Number");
}
else
{
txtans.setText("This is NOT a Prime Number");
}
if I remove the setText methods in both statements my program works, but when I leave them there and the program finds ans, then it quits, I'm wondering whats wrong with the statements? or is it not possible to use the setText method within if statements..if so how do I overcome this? What I want to do is print a string to the TextView layout when the ans = 1, any suggestions?
Yes, you can run txtans.setText() in an if statement just as well as you could run it if it wasn't in an if statement. You likely just don't have txtans initialised properly.
A quick google search brought up this as a way to print text to a textview.
Check your code, this erros usually comes when use findViewById() method in a wrong view.
In the activity you use like this findViewById(), maybe you need to call yourView.findViewById();
(If you post your class we can help you with more detailed answear.)
Also note that it is not allowed to call methods from Views from another Thread which created them. But a LogCat output including the Error will enlight us for shure :)
txtans might be NULL and you are trying to access a member of a NULL object.
Is it somehow possible that instead of:
Button btnNextWord = (Button) this.findViewById(R.id.btnNextWord);
Eclipse automatically generates for me something like:
Button btnNextWord = this.btnNextWord;
or
Button btnNextWord = R.id.getBtnNextWord(this);
??
No, because what you're doing in requesting a reference to a child element that has a certain name and Eclipse isn't actually loading in the layout xml so it can't know what is in it.
I know this is a very late response but it might help someone who read.
You can use the android annotations library for doing something similar to what you want.
As you can see on their page, you can write a field in the code like this:
#ViewById
ListView bookmarkList;
The library will findById an item with id R.id.bookmarkList and cast it to ListView.
Or you can use the annotation like this
#ViewById(R.id.myTextView)
TextView someName;
if you want to give a better name to the field.
DISCLAIMER:
I never used the library so I'm not sure whether you can use just that annotation or you're bound to use their whole set of annotations.