if statement crashing my Android application - android

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.

Related

Is there a way to trace who invoked onNext() on a Subject, in RxJava(2)?

I am developing an Android app (It doesn't matter though) using RxJava2, and in some singleton there are some PublishProcessors.
And there are a lot of .onNext() calls on these PublishProcessors all over the project.
Now in order to debug, I need to know, on every .onNext() called, which line in my project invoked this .onNext().
Is there a way in RxJava(2) that I can achieve this?
I think you can use Frames tab in Debug menu.
For example, in this case, MainActivity line 18 trigger onNext
Ah, thanks to #PhanVanLinh, I found a solution that worked for me.
(Actually it has pretty much nothing to do with RxJava...)
You just need to print the stacktrace using Thread.currentThread.stackTrace and print it to your own string inside doOnNext(), but remember to do it before .observeOn() so that the thread won't switch, it must stay at the original thread that called .onNext(), otherwise you won't get meaningful information.
Then you will know which line that called .onNext().

Getting NoMatchingViewException during perform(...), but not the preceding check(...)

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

Is it always worth checking for null pointers?

Let's say we call a method on a variable fetched through findViewById():
TextView tv = (TextView) findViewById(R.id.text);
tv.setText("Some text");
Android Studio automatically warns us that setText() may produce a NullPointerException if tv turns out to be null. If we however are certain that tv will never be null (unless something really wonky happens which should crash the app anyway), is it really worth encapsulating the method call within an if(tv != null){} statement? What if these two (or more) rows are executed very often? Can we gain any significant performance increase?
I personally don't think there is much point in checking these are null because you will know if they are present in your layout file or not, so I wouldn't bother.
However, I do sometimes use the following to prevent the warnings in Android Studio:
assert tv != null;
I don't know how this affects performance but I imagine it will have almost no difference.
Throwing or creating new exceptions will definitely consume more resources than what you might expect BUT it is always good to handle the exception where necessary rather than let the app crash. IF you are absolutely certain that the textview or whatever UI element ISpresent in the correct XML for your activity and/or the fragment then i don't think that you should use try catches at all..
Yes you can use asserts to avoid warning of Android Studio as mentioned above, and with respect to performance I don't think you will notice any huge performance variation. However, its always good to catch exceptions and log them as it can be really useful while debugging.
Actually, it can be null.
If you have complicated layout, and many ids, it can happen if you type id of other layouts.
This is only a warning. If your layout is simple to remember all ids or you sure it's never null, don't bother this warning.
This warning is helpful when you change an id in layout manually.
EX:
layout 1 has ids : text_view_1, button_1
layout 2 has ids : text_view_1, button_2
Activity 2 use layout 2 and findViewById(text_view_1)
If you change from text_view_1 to text_view_2 (MANUALLY by typing). No error happened, because id text_view_1 is still exits. But NullPointerException will occur when you run application.

NullPointerException for Radiogroup

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)))

Android. ACRA. Is putCustomData thread safe

I can't seem to find the answer to this question I'm having:
Is the method from the ACRA library ...
ACRA.getErrorReporter().putCustomData(Name, Content);
... thread safe?
I'd like to call it from two different threads and I'm not sure if i should or not.
I've searched through the documentation but I wasn't able to find anything related to this unfortunately, or maybe I'm just a bad Googleler :)
If you're not sure, buy some insurance:
ErrorReporter er = ACRA.getErrorReporter();
synchronized( er ) {
er.putCustomData( ..., .... );
}
So I think I've figure it out.
Looking through the code I've noticed that the putCustomData method is in fact a call to a HashMap's method, more precisely put.
Upon further search I've found that the method put is not synchronized.
So the answer is no, calling putCustomData from two different threads can create problems for you.
For a solution to the problem: see 323go's idea below. In my case, I already had a singleton class which I used, so I just placed a synchronized method in that class in which I called the putCustomData method.
Note: If I am wrong, someone please let me know, but this is what I was able to find out.

Categories

Resources