Converting from FragmentActivity to ActionBarActivity Results in Crash During Orientation Change

StackOverflow https://stackoverflow.com/questions/22485278

  •  16-06-2023
  •  | 
  •  

سؤال

I'm trying to convert my v4 FragmentActivity to the more recent ActionBarActivity in order to take advantage of the better backwards compatibility. However, I'm running in to an issue with orientation changes when switching from FragmentActivity to ActionBarActivity. Here is a simple example:

When I extend FragmentActivity, the following activity works without issue:

public class MainActivityBlank extends FragmentActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

        private int mCurrentPagerItem;
        private static final String NEWS_FRAG_TAG = "NEWSFRAG";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main_test);

        }


        @Override
        public void onNavigationDrawerItemSelected(int position) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            mCurrentPagerItem = position;
            //onSectionAttached(position);
            switch (position) {
                case 0: // News

                    Fragment newsFragment;
                    if (fragmentManager.findFragmentByTag(NEWS_FRAG_TAG) == null) {
                        newsFragment = new NewsListFragment();
                    } else {
                        newsFragment = fragmentManager.findFragmentByTag(NEWS_FRAG_TAG);
                    }
                    fragmentManager.beginTransaction()
                            .replace(R.id.container, newsFragment, NEWS_FRAG_TAG)
                            .commit();

                    break;
            }

        }
      }

I now try to extend ActionBarAcitivty instead. Have not changed anything else.

    public class MainActivityBlank extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

        private int mCurrentPagerItem;
        private static final String NEWS_FRAG_TAG = "NEWSFRAG";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main_test);

        }


        @Override
        public void onNavigationDrawerItemSelected(int position) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            mCurrentPagerItem = position;
            //onSectionAttached(position);
            switch (position) {
                case 0: // News

                    Fragment newsFragment;
                    if (fragmentManager.findFragmentByTag(NEWS_FRAG_TAG) == null) {
                        newsFragment = new NewsListFragment();
                    } else {
                        newsFragment = fragmentManager.findFragmentByTag(NEWS_FRAG_TAG);
                    }
                    fragmentManager.beginTransaction()
                            .replace(R.id.container, newsFragment, NEWS_FRAG_TAG)
                            .commit();

                    break;
            }

        }
    }

This results in the following error upon screen rotation:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tykin.app/com.tykin.app.common.MainActivityBlank}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
        at android.app.ActivityThread.access$900(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
        at android.app.Activity.requestWindowFeature(Activity.java:3298)
        at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:63)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        at com.tykin.app.common.MainActivityBlank.onCreate(MainActivityBlank.java:20)
        at android.app.Activity.performCreate(Activity.java:5231)

No where in my code am I calling requestFeature()

Any help would be much appreciated

هل كانت مفيدة؟

المحلول

Turns out the problem was in my NewsListFragment. Specifically, it was coming from the getActionBar call, which I did not change to support to the v7 support library. For each of my fragments, I first had to change my library imports, then clean up my code by changing

ActionBar actionBar = getActivity().getActionBar();

to

ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top