java ListFragment 支持库 GetSupportFragmentManager 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11208335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:15:08  来源:igfitidea点击:

ListFragment Support Library GetSupportFragmentManager undefined

javaandroidandroid-fragments

提问by Killerpixler

I have a list Fragment (importing the v4 support library, build target is 2.3.3 api 11) and the following problem in my code that was working fine when the target was 3.0 and I didn't use the library.

我有一个列表 Fragment(导入 v4 支持库,构建目标是 2.3.3 api 11)和我的代码中的以下问题,当目标是 3.0 并且我没有使用该库时运行良好。

Fragment frag1 = new ExampleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentframe, frag1);
ft.commit();

Now I'm getting the compile error that

现在我收到编译错误

"Type mismatch: cannot convert from ExampleFragment to Fragment"

and it tells me to change the "Fragment" to ExampleFragment (e.g.)

它告诉我将“片段”更改为 ExampleFragment(例如)

ExamplesFragment frag1 = new ExamplesFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentframe, frag1);
ft.commit();

Then I have a problem that the ft.replace command has an error:

然后我有一个问题,ft.replace 命令有错误:

The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment_PVAnn)

and it tells me to change the ExamplesFragment in the first line back to just Fragment and so the circle goes round and around

它告诉我将第一行中的 ExampleFragment 改回只是 Fragment,所以圆圈一圈一圈

回答by Alex Lockwood

You should use

你应该使用

getActivity().getSupportFragmentManager()

If that is still giving you problems, then makre sure that your Activity extends FragmentActivity.

如果这仍然给您带来问题,那么请确保您的Activity extends FragmentActivity.

If that is still giving you problems,

如果这仍然给您带来问题,

  1. Delete allof your imports,
  2. Re-import with Ctrl+ Shift+ O, and
  3. On every name clash, make sure you import from support.v4.*, notandroid.*.
  1. 删除所有导入,
  2. 重新导入与Ctrl+ Shift+ O,和
  3. 在每次名称冲突时,请确保从 导入support.v4.*而不是android.*.

回答by stuckless

You have to make sure that when you extend Fragmentthat you are extending the Fragmentthat is in the v4package space. Check your imports for Fragment, FragmentActivity, etc, and if you see them imported WITHOUT .v4 in the package name, then it is wrong.

你必须确保当您扩展Fragment您的扩展Fragment是在v4封装空间。检查你的进口FragmentFragmentActivity等等,如果你看到他们在包名无进口.v4,那是错误的。

Also, if you set the android library for the project to be 2.3 and not 4.0.x then you'll quickly see if you are using the incorrect classes, because you'll get compile errors, which can be fixed by using the v4 classes from the android support jar (which hopefully you've included in your project as well)

此外,如果您将项目的 android 库设置为 2.3 而不是 4.0.x,那么您将很快看到您是否使用了不正确的类,因为您将收到编译错误,这可以通过使用 v4 类来修复来自 android 支持 jar(希望你也包含在你的项目中)