Jamsheed Kamarudeen
Jamsheed Kamarudeen

Reputation: 728

Starting a Fragment without UI

I have this code in my activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Fragment workerfragment = new XMLworker();
    android.app.FragmentManager fm= getFragmentManager();
    FragmentTransaction fragtrans = fm.beginTransaction();
    fragtrans.add(workerfragment, "work");
    fragtrans.commit();
  }

The fragments name is XMLWorker. It doesnot have a UI and i use it to parse some xmls.

On this line of my code

 fragtrans.add(workerfragment, "work");

I get the error

The method add(Fragment, String) in the type FragmentTransaction is not applicable for the arguments (Fragment, String).

why? What am i doing wrong?

EDIT: is it because i am using

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

??

Upvotes: 2

Views: 4297

Answers (3)

Braian Coronel
Braian Coronel

Reputation: 22867

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    binding = DataBindingUtil.inflate(inflater, R.layout.my_layout, container, false)

    ...

    binding.root.visibility = View.GONE
    return binding.root
}

GL

Source

Upvotes: 0

Bogdan Alexandru
Bogdan Alexandru

Reputation: 5552

You are mixing up the standard, built-in Fragment APIs with the support.v4 APIs which should only be used for apps that target older Android versions (API version 4+ as its name implies).

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

The method add(Fragment, String) in the type FragmentTransaction is not applicable for the arguments (Fragment, String).

The error means that type mismatch:

In your case fragtrans refers to the native Fragment support while workerfragment to Fragment from the support package. Check you import. You are using mixed imports for fragment from the support package and the native one.

Instead of

 android.app.FragmentManager fm= getFragmentManager();

you should use

FragmentManager fm =  getSupportFragmentManager();

Upvotes: 2

Related Questions