Default constructor is deprecated, I can't find the fragment problem in Adroid Studio

Viewed 1075

When I write the Code like this, the ControlFragment works fine, but when I use the exact same Code in my other Fragment named "AndresFragment" (second code), it's not working. It says "Default constructor in android.app.Fragment is deprecated". I really don't get it... Can someone please help me, I'm new in

package com.example.fragm;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class ControlFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (savedInstanceState != null) {

        }
        View myView = inflater.inflate(R.layout.controlfragment,
                container, false);
        return myView;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

}


Code for "AndresFragment":

package com.example.fragm;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AndresFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (savedInstanceState != null) {
// restore internal values if necessary
        }
        View myView1 = inflater.inflate(R.layout.andresfragment,
                container, false);
        return myView1;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}

That's my Mainactivity.java with the ControlFragment it works:

package com.example.fragm;

import androidx.appcompat.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);
        if (savedInstanceState != null) {
            return;
        }
        ControlFragment myFragment = new ControlFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.my_fragment_container, myFragment).commit();
    }
}

But I can't use "AndresFragment"

package com.example.fragm;

import androidx.appcompat.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);
        if (savedInstanceState != null) {
            return;
        }
        AndresFragment myFragment = new AndresFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.my_fragment_container, myFragment).commit();
    }
}

When I try to compile it, the error is: "error: no suitable method found for add(int,AndresFragment) getSupportFragmentManager().beginTransaction().add(R.id.my_fragment_container, myFragment).commit();"

1 Answers

You have wrong import in your AndresFragment.

Instead of:

import android.app.Fragment;

You should use:

import androidx.fragment.app.Fragment;

In your first file you have proper import and that is why you don't have error there, android.app.Fragment is old and deprecated so that is why you receive error message since you should be using new one.

Related