In the “ActionBarTabsPager” tutorial, getActivity returns null

不羁的心 提交于 2020-01-25 12:08:13

问题


I have successfully implemented the tutorial: http://developer.android.com/reference/android/support/v4/view/ViewPager.html, as a Tab'ed viewpager activity with fragments on each tab. Each Fragment maintains various UI TextFields etc and everything is working fine with the exception of getActivity(), which returns null when called from any of the fragments.

UPDATE: Read this, then please see my own answer below that broadens the scope regarding the cause of this error. Continued:

BUT, the null status appears after a while. Initially, in fragment.onStart(), the getActivity() is working so that the default UI setup may be performed. But the first time the user has made changes, getActivity() already returns null.

Strange to say, in the same moment, it is still possible to make any change to the fragment UI fields from the Activity, which means that as the context=activity is passed to the fragment in a setSomeText(this, ...), this will enable the fragment to make the corresponding changes. Of course, the design should be such that the Fragment takes care of it's own detailed task.

It does not help to save the context in the onStart(), because that reference will point to a null after a while.

It is explicitely stated in the tutorial that the feature is in early development, but as this "null" problem has become quite a timethief here, and as I see that "getActivity returns null" is a very common problem, I wanted to muse aloud whether there could be a bug in the getActivity() when combined with ViewPager and/or Tab?

What took me so long to detect the problem was that it is hard to guess that a fragment would EVER loose knowledge of its activity. Anyway, I am on the next hurdle and just wanted to share this finding: Don't trust getActivity(), but pass on context from Activity to its Fragments as a parameter in the set/get methods or other api.


回答1:


This is not an answer, but I needed the space to explain and I am circeling in the problem:

It seems there is more general problem than just getActivity(). Because variable declarations of the fragments are also "vanishing" to null. A new instance of the fragment has "taken over". This happens when current tab is shifted more than one tab to either side.

EXAMPLE: I define 5 tabs. I have a tab 2 that can be manipulated from UI. After a change of 2 content, I move between tabs, either with tab click or fingersweep. Either way.

RESULTS? As long as I visit the next tab on left or right side, and then move back, the changed data are still there on tab 2. As soon as I move two or more tabs away from tab 2 and then return, the fragment instance of tab 2 is always reset. Does not matter how many tabs are present. Whether I hit last or first tab during this process is not significant. The code? it is the same as in the referenced tutorial, and in addition:

//add tabs (notice the once only saving of the fragment into profileViewer)

    mTabsAdapter.addTab(actionBar.newTab()
            //.setText(R.string.action_favorite)
            .setIcon(R.drawable.ic_action_favorite),
            TabFragmentDemo.class, null);

    mTabsAdapter.addTab(actionBar.newTab()
            //.setText(R.string.new_profile)
            .setIcon(R.drawable.ic_action_add_person),
            ProfileViewer.class, null); 
    profileViewer = (ProfileViewer) mTabsAdapter.getItem(NEW_PROFILE);

    mTabsAdapter.addTab(actionBar.newTab()
            //.setText(R.string.action_select)
            .setIcon(R.drawable.ic_action_view_as_list),
            TabFragmentDemo.class, null);

    mTabsAdapter.addTab(actionBar.newTab()
            //.setText(R.string.action_select)
            .setIcon(R.drawable.ic_action_view_as_list),
            TabFragmentDemo.class, null);

    mTabsAdapter.addTab(actionBar.newTab()
            //.setText(R.string.action_select)
            .setIcon(R.drawable.ic_action_view_as_list),
            TabFragmentDemo.class, null);

Then a simple dialogFragment selecting a date and then this date is set (Does not help to omit the datepicker and just set a date directly)

 public void showDatePickerDialog(View v) {     

    dateOfBirthPicker = new DateOfBirthPicker();       
    dateOfBirthPicker.show(fragmentManager, datePickerTag); 

}

//the callback from datepicker:
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    //update the fragment     
    profileViewer.setCardFromDate (this, day, month, year);

Notice that "this" is passed on as forced context to the fragment. The big question here is why the tab looses its original fragment as long as there is nothing in my code requesting that ?



来源:https://stackoverflow.com/questions/22245048/in-the-actionbartabspager-tutorial-getactivity-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!