0

I'm extending SherlockFragmentActivity. When I inspect the element this.getFragmentManager() I obtain the following description.

FragmentManager android.app.Activity.getFragmentManager()

public FragmentManager getFragmentManager () 
Added in API level 11
Return the FragmentManager for interacting with fragments associated with this activity.

This means that I can use it only for API level 11 or more.

In fact the following code give me an error:

android.app.FragmentManager fragmentManager = this.getFragmentManager();
android.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
DetailsServiceListFragment fragment = new DetailsServiceListFragment();
fragmentTransaction.add(R.id.dettservizio,fragment);

where DetailsServiceListFragment extends SherlockListFragment

The error is on the fourth line, and says:

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

This happens because DetailsServiceListFragment need to extend Fragment. But why I can't extend SherlockListFragment?

When I use the classes included in Sherlok project what Fragment I have to use?

4

3 回答 3

0

您应该改用该getSupportFragmentManager()方法。正如您所指出的android.app.*,与 Fragment 相关的类仅适用于 API 11+。

android.support.v4.app.FragmentManager fragmentManager = this.getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
DetailsServiceListFragment fragment = new DetailsServiceListFragment();
fragmentTransaction.add(R.id.dettservizio,fragment);

另外,不要忘记提交您的FragmentTransaction

fragmentTransaction.commit();

DetailsServiceListFragment extends SherlockListFragment,并且SherlockListFragment已经扩展android.support.v4.app.Fragment,所以很好,保持原样。

于 2013-10-16T21:31:05.513 回答
0

您需要同时使用Fragment(ie android.support.v4.app.Fragment)的支持库版本getSupporFragmentManager()。如果需要,您现在可以使用 Android 自己的 ActionBar 反向移植。

于 2013-10-16T21:31:29.417 回答
0

此时,我建议使用 ActionBarCompat 库(它是 Google 官方支持库 - 版本 7 的一部分)。

他们花了很长时间来创建一个向后兼容的 Actionbar,但现在它已经存在,您应该使用它而不是 ActionBarSherlock。

这是 API:http: //developer.android.com/reference/android/support/v7/app/ActionBar.html

...和一个很好的介绍: https: //plus.google.com/108967384991768947849/posts/9Tw5zZvAGnq

于 2013-10-16T21:54:27.360 回答