gpt4 book ai didi

android - 从 FragmentActivity 转换为 ActionBarActivity 导致在方向更改期间崩溃

转载 作者:行者123 更新时间:2023-11-30 03:01:23 24 4
gpt4 key购买 nike

我正在尝试将我的 v4 FragmentActivity 转换为更新的 ActionBarActivity,以便利用更好的向后兼容性。但是,从 FragmentActivity 切换到 ActionBarActivity 时,我遇到了方向更改的问题。这是一个简单的例子:

当我扩展 FragmentActivity 时,以下 Activity 可以正常工作:

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() 的地方

任何帮助将不胜感激

最佳答案

原来问题出在我的 NewsListFragment 中。具体来说,它来自 getActionBar 调用,我没有更改它以支持 v7 支持库。对于我的每个 fragment ,我首先必须更改我的库导入,然后通过更改清理我的代码

ActionBar actionBar = getActivity().getActionBar();

ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();

关于android - 从 FragmentActivity 转换为 ActionBarActivity 导致在方向更改期间崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com