Problem with tab view, cannot set content view
-
21-01-2021 - |
Domanda
I'll link all of my files in the bottom of my post
My problem is that, I have a tab view, currently with two tabs. These tabs are each a seperate activity, which extends CustomMenuActivity. This is so that both activities can share the same options menu.
In my options menu, I have a previous button, so that, that calls another method, that sets my content view. That gives me a NullPointerException.
It has something to do with the fact that I am setting my content view inside another method.
Is there any way to fix that, or work around it somehow?
http://pastebin.com/PR7uMTYC - XML file
http://pastebin.com/VsMrMggn - GuideActivity.java
http://pastebin.com/YVdS6P4j - JAndroidTroubleShooter.java
http://pastebin.com/CpjwM07j - CustomMenuActivity.java
Soluzione
Provided that the menu you want in all tabs is exactly the same you can do as follows:
- Instead of having each Tab inflate its own menu - inflate the menu on the TabActivity itself.
- Create an interface that each Tab can implement, declaring the methods that the menu will call on the different Tabs.
In your TabActivity you can get the current visible Tab like this:
Activity activity = getLocalActivityManager().getActivity(getTabHost().getCurrentTabTag());
Then you make sure that the activity in question implements your interface and call the relevant method:
if (activity instanceof MyTabInterface)
((MyTabInterface) activity).callThisMenuMethod();