0
目标

我正在尝试编写一种方法来替换我用来交换片段的代码,以便将复制和发布保持在最低限度(并保持干燥)

问题

当我尝试使用作为参数传入的类来创建该类的新实例时出现错误。

错误发生在这行代码中,在运算符(等号)的左侧:

newFragmentClass new_fragment = newFragmentClass.newInstance();     

它给我的错误是:“newFragmentClass 无法解析为类型”。

完整代码
    private void changeFragment(Class<?> newFragmentClass)
    {
        // Detect the current fragment
            Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    
        // Create a new instance of the given class, edit later to gracefully handle errors
            newFragmentClass new_fragment = newFragmentClass.newInstance(); 
            
        // Switch to the new fragment
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.detach(current_fragment);
            transaction.replace(R.id.fragment_container, new_fragment);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.commit();
            
        // Change the tab background to indicate that the tab is active, reset backgrounds for any other tabs
            findViewById(R.id.page_one_tab).setBackgroundResource(R.drawable.menu_button_background_active);
            findViewById(R.id.page_two_tab).setBackgroundResource(R.drawable.menu_button_background);
            findViewById(R.id.page_three_tab).setBackgroundResource(R.drawable.menu_button_background);
    
    }
    
    // Page One Tab Button Functionality
    public void pageOneTab (View v)
    {
        // Change the fragment to SelectPlayersFragment
        changeFragment(Page_One_Fragment.class);
    }
尝试的解决方案

我已经在 StackOverflow 和整个互联网上搜索了很长一段时间,但一直没有找到解决方案。像这样的一些主题似乎可以解决问题,但后来我在 transaction.replace 行上遇到了一个错误,我找不到修复方法:“FragmentTransaction 类型中的方法 replace(int, Fragment)不适用于参数(int,Object)”。

4

4 回答 4

1

因为你的“newFragmentClass”只是方法的一个参数,它不是一个类型,所以你不能实例化它。按照我的代码来解决你的问题

private <T extends Fragment> void changeFragment(Class<T> newFragmentClass)
    {
        // Detect the current fragment
            Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.content);

        // Create a new instance of the given class, edit later to gracefully handle errors
            T new_fragment = newFragmentClass.newInstance(); 

        // Switch to the player select fragment
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.detach(current_fragment);
            transaction.replace(R.id.fragment_container, new_fragment);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.commit();

        // Change the tab background to indicate that the tab is active, reset backgrounds for any other tabs
            findViewById(R.id.page_one_tab).setBackgroundResource(R.drawable.menu_button_background_active);
            findViewById(R.id.page_two_tab).setBackgroundResource(R.drawable.menu_button_background);
            findViewById(R.id.page_three_tab).setBackgroundResource(R.drawable.menu_button_background);

    }
于 2013-08-29T07:58:36.600 回答
1

你可能想要这样的东西:

private Fragment changeFragment(Class<? extends Fragment> newFragmentClass)  {
    Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    // Create a new instance of the given class, edit later to gracefully handle errors
    Fragment new_fragment = null;
    try {
         new_fragment = newFragmentClass.newInstance();
    } catch (InstantiationException e) {            
        throw new RuntimeException(e); // for some reason this fragment loading has failed so crash
    } catch (IllegalAccessException e) {            
        throw new RuntimeException(e);
    } 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.detach(current_fragment);
    transaction.replace(R.id.fragment_container, new_fragment);
    // ...   
于 2013-08-29T07:54:04.893 回答
1

也许更简单的解决方案是将 aFragment作为参数而不是 a传递Class,并使用它来替换当前片段。此外,您不需要分离当前片段,这replace()对您有用。

像这样的东西:

public void changeFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.fragment_container, fragment, "tag");
    transaction.commit();
    //.....
}

你像这样使用它:

changeFragment(new Page_One_Fragment());
于 2013-08-29T07:54:31.977 回答
1

我想你想要这个:

private <T extends Fragment> void changeFragment(Class<T> newFragmentClass)
{
    ...
    // Create a new instance of the given class, edit later to gracefully handle errors
    T new_fragment = newFragmentClass.newInstance(); 
    ...
}
于 2013-08-29T07:55:31.560 回答