问题
I have an app structure like this:

Background:
The Parent activity #1 is just holding the tab host, tab widget and is also setting up the 3 tabs to have their content set to the 3 tab
activities
(tab1, tab2, tab3 activities).Update: I tried calling my validation method inside onTabChangedListener in Parent Activity #1 but I got a Null Pointer Exception. Couldn't really trace it to anywhere. I did commented/deleted conflicting code but still I am not getting the bottleneck.StackTrace(PasteBin Link). Code for Parent Activity #1,Tab#1 Activity
Problem:
I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener
.
Am I missing something here?
The listener(s) are set in their own tab# activities under oncreate
method.
Apart from trying the above technique, I had tried setting up the listener in onResume()
under the main Parent activity #1. But the on Resume()
method was never invoked. I got a null pointer exception too.
Idea behind validation being: I want that while the user is changing tabs, the data should be validated before he can skip over a tab. So, ineffect I would require tab#1 to validate data in a event similar to onTabChanged
if tab#2/tab#3 is selected.
Also, this would apply if current tab#2 is selected and user selects tab#1/tab#3
Any advice will be appreciated..
Thanks for reading..
回答1:
I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener.
There is no need for a second OnTabChangeListener
and even if you could set it it wouldn't help you. As you constructed the code you need access to the child activities. You can do this by using one of the answers in this question. The problem is, that those answers, except the accepted one, use deprecated methods.
My method, that I proposed in the comments is to have a static boolean field in each of the child activities used as tabs and let all of your activities update that boolean flag whenever there is a change of state of the views in those activities(if you check a CheckBox
, enter something in an EditText
etc). Then you can simply check the flag for the desired child activity in the OnTabChangeListener
. My method should work but your code is a bit messy so you would have to modify it quite a bit.
I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.
It's normal that you get a NullPointerException
with your code as I haven't seen where you initialize the references to the child activities that you use in the OnTabChangeListener
.
Also:
Don't use TabActivity
. It's been deprecated in favor of the Fragments
framework which is more flexible. Those fragments could help you because, I think you want to stop changing the tabs if the validation of the current page fails and the OnTabChangeListener
might come a bit late for that(but I may be mistaken about what you want).
As a side note, use equals
in your code to test String
equality and not ==
.
来源:https://stackoverflow.com/questions/14274110/how-to-validate-input-data-in-a-tab-on-tab-change