0

In my app I have two views, a main and a secondary. However, if my app is paused while in the second view it crashes when the on resume code runs. I discovered that this was because in my on resume has a line where it sets a value to a textview. I would like to encase it in an if statement, but I do not know what phrase I should use to find the content View. Sorry if I am a little unclear - when my user is in secondary view and they close then reopen the app, running the on resume code, it crashes because the text view that the onResume method is trying to modify cannot be accessed.(as it is my main view) and so it crashes.

I know that I could use a try-catch statement, but I also need to find the view in a place in my app where I can't use a try-catch.

jcw
  • 5,132
  • 7
  • 45
  • 54

2 Answers2

0

Please post the relevant code and logcat errors to clarify your question.

I'm going to take a guess though. If you want to find a view displayed in an Activity, simply use:

TextView text = (TextView) findViewById(R.id.text);
if(text != null) {
    text.setText("Something");
}

However if it was visible before the second Activity was launched, normally it will be visible when this first Activity resumes, so I don't understand what is happening to make the TextView null...

Sam
  • 86,580
  • 20
  • 181
  • 179
0

I solved my problem by keep in an int value at zero whilst in one view, and whenever the view is switched, I changed the value of that int to one. Then in my onResume, if that ints == 1, then I don't set the value of my edittext, but if that int == 0, then I know that it is safe to call the setText() method

jcw
  • 5,132
  • 7
  • 45
  • 54