Use getSupportFragmentManager() Inside PreferenceActivity

Deadly 提交于 2019-12-23 10:18:11

问题


I have my custom ConfigActivity which extends PreferenceActivity (from android.preference.PreferenceActivity). The layout for this activity contains a <ListView android:id="@android:id/list".... Inside this layout, I need to programmatically add/replace a couple of other Fragments I have created that extend android.support.v4.app.Fragment.

However, Eclipse says:

The method getSupportFragmentManager() is undefined for the type ConfigActivity.

I tried replacing it for just getFragmentManager(), but the following call:

ft.add(R.id.fHeader, new MyCustomFragment());

results in the following error:

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

Is my only option to NOT use my custom Fragments that extends the support library's Fragment class inside ConfigActivity?


回答1:


I think you will need to make your activity extend FragmentActivity or some subclass of FragmentActivity to work with Fragments if you are using android.support.v4.app.Fragment. Other option can be to make your minimum sdk version set to 12 and check your code without using support libraries.




回答2:


Usual Activity do not have FragmentManager. If you want to use Fragment in your Activity, you should use FragmentActivity which is in support libs.
Because android is open source, you can use the code in PreferenceActivity, and make a MyPreferenceActivity extends FragmentActivity. Hope it will be helpful :)



来源:https://stackoverflow.com/questions/19493983/use-getsupportfragmentmanager-inside-preferenceactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!