Wrong 2nd argument type. Found: 'com.example.asus.vidasegura.SesionFragment', required: 'android.support.v4.app.Fragment'

0

I get that error when trying to call a fragment in android studio.

Continuation of the error:

replace(int,android.support.v4.app.Fragment)
in FragmentTransaction cannot be applied to
(int, com.example.asus.vidasegura.SesionFragment)

MainActivity.java

package com.example.asus.vidasegura;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        FragmentManager fm=getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.escenario, new SesionFragment()).commit();
    }
}

SesionFragment.java

package com.example.asus.vidasegura;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;




public class SesionFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sesion, container, false);
    }
}

Error throws me to new SesionFragment() in MainActivity.java

What will this error be? : (

    
asked by nicolasyo1WWE 24.07.2018 в 02:10
source

2 answers

0

The error simply states that to use the replace(int,android.support.v4.app.Fragment) method you need a Fragment of the class:

import android.support.v4.app.Fragment;

and not the class

import android.app.Fragment;

Change the Import so that your Fragment SesionFragment extends from android.support.v4.app.Fragment :

package com.example.asus.vidasegura;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
//import android.app.Fragment;
import android.support.v4.app.Fragment; //*Agrega!
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;




public class SesionFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sesion, container, false);
    }
}
    
answered by 24.07.2018 в 17:45
0

What happens is that you are using the SupportFragmentManager , which only handles the Fragments of the package android.support.v4.app .

In your SesionFragment , try importing:

import android.support.v4.app.Fragment;

instead of:

import android.app.Fragment;

This you must do with all the other Fragments that you have created, if you are going to use the SupportFragmentManager in the Activity that handles your Fragments .

    
answered by 24.07.2018 в 02:21