[英]wrong 2nd argument type. required: 'android.support.v4.app.Fragment'
The error im getting is: Wrong 2nd arguement type. 我得到的错误是:错误的第二争论类型。 found: 'com.example.appname.MainmenuFragment', required: 'android.support.v4.app.Fragment' By the way I know a similar question has been asked but that case is slightly different because this code is written in MainmenuActivity which is an activity not a fragment and MainmenuFragment is a fragment as the name implies. 找到:“ com.example.appname.MainmenuFragment”,必需:“ android.support.v4.app.Fragment”顺便说一句,我知道有人问过类似的问题,但是这种情况略有不同,因为此代码是用MainmenuActivity编写的,顾名思义,是一个活动而不是一个片段,而MainmenuFragment是一个片段。 It worked this morning. 今天早上工作了。 I've been exploring quite a lot today but ended up having this unusual error. 我今天进行了很多探索,但最终遇到了这个异常错误。 Here is the code in which I am getting the error: 这是我收到错误的代码:
public class MainmenuActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_mainmenu:
transaction.replace(R.id.container, new MainmenuFragment()).commit();
return true;
}
return false;
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmenu);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new MainmenuFragment()).commit();
}}
the line 线
transaction.replace(R.id.container, new MainmenuFragment()).commit();
and 和
transaction.replace(R.id.container, new MainmenuFragment()).commit();
is where I am getting the error. 是我得到错误的地方。 To be honest if I try using other fragment files instead of Mainmenu_Fragment it works fine wierdly. 老实说,如果我尝试使用其他片段文件而不是Mainmenu_Fragment,它将可以很好地工作。
您可能在MainmenuFragment
使用了android.app.Fragment
而不是android.support.v4.app.Fragment
The problem might be because of getSupportFragmentManager()
. 问题可能是由于getSupportFragmentManager()
。 Try changing it to getFragmentManager()
. 尝试将其更改为getFragmentManager()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.