我正在尝试编写一种方法来替换我用来交换片段的代码,以便将复制和发布保持在最低限度(并保留D.R.Y。)
问题当我尝试使用我作为参数传入的类来创建该类的新实例时,我收到错误。
错误发生在这行代码中,位于运算符的左侧(等号):
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和互联网很长一段时间,但却找不到解决方案。一些主题like this one似乎可以解决问题,但后来我在transaction.replace行遇到了一个错误,我找不到修复:“方法替换(int,Fragment)中的type FragmentTransaction不适用于参数(int,Object)“。
答案 0 :(得分: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);
// ...
答案 1 :(得分:1)
或许更简单的解决方案是传递Fragment
作为参数,而不是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());
答案 2 :(得分: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();
...
}
答案 3 :(得分:1)
因为你的&#34; newFragmentClass&#39;它只是方法的一个参数,它不是一个类型,所以你不能实例化它。 按照我的代码来解决您的问题
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);
}