last day (14 days later) » 

7:25 AM
1
Q: Tab fragment changing action bar spinner

YawarI have fragment activity, with tab fragments. like this. But when ever I tap on second tab, spinner on action bar becomes empty, like this. I think it would be a common problem, but if it is not tell me I'll provide some code also. I'm not doing any thing which is related to it in on tab c...

 
Are you changing a fragment onTab click?
 
I have android.support.v4.app.FragmentTabHost in xml file, and then adding tabs like this
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.addTab( ‌​‌​ mTabHost.newTabSpec("tab1").setIndicator( getResources().getString(‌​R.‌​string.interest)), InterestTabFragment.class, null);
 
What is your InterestTabFragment.class? Is it activity or fragment ?
 
Its a fragment.
I have added on tab changed code, please have a look on it. May be some thing is wrong with it.
 
Can you post ypur FragmentActivity code ?
 
7:25 AM
Which part? as its too much
 
where have you populated actionBar spinner with data?
 
okay I'm editing my question
I have edited my question now
 
onCreateOptionMenu method is in activity or fragment?
 
activity
 
means you have shown action bar in activity right?
 
7:40 AM
Yes, right
 
7:56 AM
have you called setHasOptionsMenu(true) in a fragment?
 
no
 
ok
once try calling it
 
where should i call this ?
 
onCreateView method of Fragments
 
Ok, Let me try this.
 
8:15 AM
this is Not solving
my issue
:(
 
9:03 AM
I think there is some problem in logic
is your fragment changing properly when clicked on tab?
 
9:29 AM
instead of calling setAdapter in onPostExecute try calling YearsSpinnerAdapter.notifyDataSetChanged();
 
9:51 AM
yes fragment is changing
no problem in fragment, except for that spinner problem
ok I'll check it now
 
10:32 AM
this is throughing null exception
 
YearsSpinnerAdapter must not be initialized
 
I didn't understand?
 
you have to initialize adapter before setting it to spinner
Ex. YearsSpinnerAdapter adpter = new YearsSpinnerAdapter(this, yearsList);
 
Okay I'm setting it above, then check it
Still same problem, check my answer, at the I have given the yearspinnerAdapter. Then tell me where should I place it? I have now placed it above all methods. But still its giving null exception
 
10:55 AM
your answer is not yet updated
 
Can't you see that code snippet ?

private SpinnerAdapter YearsSpinnerAdapter = new BaseAdapter() {

private TextView text;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertV
...
 
can you give me project?
 
11:14 AM
in wich line u r getting nullpointer?
 
Can't, as its not my own project. I am just working on it.
on post execute,
only when i write this.
YearsSpinnerAdapter.notifyDataSetChanged();
without this line, no null exception
So much annoying problem
 
what happens when u put nothing in onPostExecute?
 
Let me check
If i do this, spinner item will be empty even in first fragment,
where it was working ok
I have checked one more thing, may be that helps you understand
 
k
what?
 
i have put two breakpoint in oncreateview
one on start and one on end
on frist breakpoint ((ProfileActivity)getActivity()).spinner.getAdapter()
this line shows that
it has a adapter
but on second breakpoint
((ProfileActivity)getActivity()).spinner.getAdapter()
shows that it doesn't have a adapter
after that break point i added this code
// if(((ProfileActivity)getActivity()).spinner.getAdapter() == null)
// {
// ((ProfileActivity)getActivity()).spinner.setAdapter(((ProfileActivity)getActivity()).YearsSpinnerAdapter);
// }
to again set adapter
 
11:22 AM
can u show me onCreate?
 
but this is still not showing adapter
on create of what ?
activity or fragment ?
 
Activity?
 
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.activity_profile);
initUI();

// Check for session and request API Call.
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// Get the user's data
AccessToken = session.getAccessToken();
makeStoriesRequest(session);
makeInterestRequest(session);
makeHangoutRequest(session);
makeInteractionRequest(session);
 
ok so u were trying that breakpoints in fragments onCreateView??
 
yes
 
11:25 AM
put that code in activity
 
in activity' on create method /
?
 
so that items list will remain as it is even when fragments changes
yes
 
ok you want to say that, when other fragment starts, it call on create of its activity ?
 
no no
as that single activity contains all your fragments, action bar operations should be kept in activity
so that it remains consistent throughout fragments
 
12:16 PM
I have at last got why the problem is occurring, thanks for your so long and consistent help. Some who you made it possible to solve.
 
now is it working?
 
yup
i just put that code in onoptioncreatemenu function
// if(((ProfileActivity)getActivity()).spinner.getAdapter() == null)
// {
// ((ProfileActivity)getActivity()).spinner.setAdapter(((ProfileActivity)getActivity()).YearsSpinnerAdapter);
// }
 
great.. means moving that code to activity worked?
 
yes but not in on create,
I moved it in onoptioncreatemenu
function
 

last day (14 days later) »