java - RoboFragment 不适用于 FragmentTransaction.add(int,Fragment) 方法

标签 java android android-fragments guice roboguice

我有

public class ItemDetailFragment extends RoboFragment

我尝试从另一个 Activity 创建它:

public class ItemDetailActivity extends RoboFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_detail);

        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // savedInstanceState is non-null when there is fragment state
        // saved from previous configurations of this activity
        // (e.g. when rotating the screen from portrait to landscape).
        // In this case, the fragment will automatically be re-added
        // to its container so we don't need to manually add it.
        // For more information, see the Fragments API guide at:
        //
        // http://developer.android.com/guide/components/fragments.html
        //
        if (savedInstanceState == null) {
            // Create the detail fragment and add it to the activity
            // using a fragment transaction.
//            Bundle arguments = new Bundle();
//            arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
//                    getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
            ItemDetailFragment fragment = new ItemDetailFragment();
            //fragment.setArguments(arguments);
            getFragmentManager().beginTransaction()
                    .add(R.id.item_detail_container, fragment)
                    .commit();

            fragment.init(getApplicationContext());
        }
    }

但我收到此错误:

Error:(45, 21) Gradle: error: no suitable method found for add(int,ItemDetailFragment)
method FragmentTransaction.add(int,Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.add(int,Fragment) is not applicable
(actual argument ItemDetailFragment cannot be converted to Fragment by method invocation conversion)
method FragmentTransaction.add(Fragment,String) is not applicable
(actual argument int cannot be converted to Fragment by method invocation conversion)

最佳答案

据我所知,RoboFragmentActivity 从支持库扩展了android.support.v4.app.FragmentActivity

我认为您应该调用 getSupportFragmentManager() 而不是 getFragmentManager()

关于java - RoboFragment 不适用于 FragmentTransaction.add(int,Fragment) 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28004692/

相关文章:

java - 如何在 Android 中为按钮文本使用两种不同的字体大小?

java - Android Studio : How to configure Java module to contain embedded resources?

java - 如何使用 Jericho 解析没有嵌套 html 元素的文本?

android - 如何展开和折叠 ListView 中的项目

android - 在 Activity 和 Fragment 之间传递数据

java - 隐藏列但未读取正确的值

android - 如何访问我在收件箱中收到的短信时间?

c# - (Unity 2D)在离开屏幕时销毁实例化的预制件?

java - 如何使用 echo php 到我的 android fragment

android - 使用 fragment 返回堆栈处理 ActionBar 标题?