gpt4 book ai didi

java - 从非 Activity 课中打开 Sherlock Fragment Activity ?

转载 作者:行者123 更新时间:2023-12-01 11:08:33 25 4
gpt4 key购买 nike

我正在尝试从非 Activity 类中打开 SherlockFragment Activity 在正常 Activity 中使用它

Android.app.Activity >> NonActivityClass >> SherlockFragmentActivity

将其从 Activity 类传递到 NonActivityClass 并尝试启动 SherlockFragmentActivity

代码 -

private void changeFragment(Activity act) {

Fragment profileFrag = new New_ProfileFragment();
FragmentTransaction transaction = ((SherlockFragmentActivity) act)
.getSupportFragmentManager().beginTransaction();

try {
((SherlockFragmentActivity) act).getSupportFragmentManager()
.popBackStackImmediate(profileFrag.toString(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
} catch (java.lang.IllegalStateException e) {

}

transaction.addToBackStack(profileFrag.toString());
transaction.replace(R.id.content_frame, profileFrag);
transaction.commitAllowingStateLoss();
((SherlockFragmentActivity) act).getSupportFragmentManager()
.executePendingTransactions();

}

我遇到错误

FragmentTransaction transaction = ((SherlockFragmentActivity) act)
.getSupportFragmentManager().beginTransaction();

异常 - java.lang.ClassCastException: Activity 无法转换为 com.actionbarsherlock.app.SherlockFragmentActivity

最佳答案

a.您知道 ActionBarSherlock 已被弃用 2 年吗?

b.

activity cannot be cast to com.actionbarsherlock.app.SherlockFragmentActivity

几乎说明了一切。您无法将 android.app.Activity 转换为 SherlockFragmentActivity,因为 what I can see SherlockFragmentActivity 不扩展 android.app.Activity

c.你要么必须做

activity.getFragmentManager()

或者,如果您必须使用 SupportFragmentManager,您的第一个 Activity 必须是 android.support.v4.app.FragmentActivity,这样您就可以执行

activity.getSupportFragmentManager()

无论哪种方式,摆脱ActionBarSherlock

关于java - 从非 Activity 课中打开 Sherlock Fragment Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645753/

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