RM3
RM3

Reputation: 107

Adding a fragment, a "wrong second argument type" error occurs

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();

    }

  }
}

Upvotes: 2

Views: 3677

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

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.

Upvotes: 7

Related Questions