Adding a fragment, a “wrong second argument type” error occurs

旧城冷巷雨未停 提交于 2020-06-26 12:19:00

问题


I have checked similar posts and their solutions do not seem to work. I am (obviously) new to this, so it may just be a lack of skill on my part.

I am receiving the error:

"Wrong 2nd argument type Found:'mycompany.fragment_test... required: 'android.support.v4.app.Fragment

replace (int, android.support.v4.ap.Fragment) to (int, mycompany.fragment_test...)'

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;


public class MainActivity extends FragmentActivity {

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

    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        Menu_Fragment fragment = new Menu_Fragment();
        fragment.setArguments(getIntent().getExtras());

        

        ft.replace(R.id.fragment_container, fragment); //This is the line with the issue
        ft.commit();

    }

  }
}

回答1:


Menu_Fragment is not inheriting from android.support.v4.app.Fragment. Presumably, it is inheriting from android.app.Fragment.

There are two fragment implementations: the native one (e.g., android.app.Fragment) and the backport (e.g., android.support.v4.app.Fragment). You need to be consistent. Your activity is a FragmentActivity, which is part of the backport, so you need your fragments to inherit from android.support.v4.app.Fragment.



来源:https://stackoverflow.com/questions/33878779/adding-a-fragment-a-wrong-second-argument-type-error-occurs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!