我正在尝试将我的 v4 FragmentActivity 转换为更新的 ActionBarActivity 以利用更好的向后兼容性。但是,从 FragmentActivity 切换到 ActionBarActivity 时,我遇到了方向更改的问题。这是一个简单的例子:
当我扩展 FragmentActivity 时,以下活动可以正常工作:
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;
}
}
}
我现在尝试扩展 ActionBarAcitivty。没有改变任何其他东西。
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;
}
}
}
这会在屏幕旋转时导致以下错误:
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)
在我的代码中没有我调用 requestFeature()
任何帮助将非常感激