我正在尝试替换我的一个 fragment ,但由于这些错误我无法这样做。但是我已经有了这些导入,因此我不知道这里出了什么问题。
错误1
Error:(73, 96) error: incompatible types
required: android.support.v4.app.FragmentTransaction
found: android.app.FragmentTransaction
错误2
Error:(72, 48) error: incompatible types
required: Fragment
found: FragmentMap
导入
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
fragment 替换代码
Fragment newFragment = new FragmentMap();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
最新截图
最佳答案
对于错误1
Error:(73, 96) error: incompatible types
required: android.support.v4.app.FragmentTransaction
found: android.app.FragmentTransaction
将 getFragmentManager()
更改为 getSupportFragmentManager()
它应该可以工作。
getSupportFragmentManager()
is used when you are using support lib (your case). andgetFragmentManager()
is used when not using support lib.
对于错误2
Error:(72, 48) error: incompatible types
required: Fragment
found: FragmentMap
因此要替换 fragment ,请使用以下命令:
Fragment newFragment = new FragmentMap();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
newFragment
是要替换的 fragment ,但会引发错误。
Make sure the
FragmentMap
or any other fragment to be replaced, is extended by Fragment class (of support lib and not the normal one, becausenewFragment
is an object ofFragment
from support lib) usingimport android.support.v4.app.Fragment;
and notimport android.app.Fragment;
关于java - fragment 替换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30414749/