Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

zarah hafeez
zarah hafeez
317 Points

Getting error in FragmentTransaction fragmentTransaction fragmentManager.beginTransaction();

Find below code:

package com.example.i7.fragments;

import android.content.res.Configuration; import android.support.v4.app.Fragment; import android.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Configuration config = getResources().getConfiguration();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction =
    fragmentManager.beginTransaction();

    if (config.orientation== Configuration.ORIENTATION_LANDSCAPE){
        LM_Fragement lm_fragement= new LM_Fragement();
        fragmentTransaction.replace(android.R.id,lm_fragement);
    }
    else{
        PM_Fragment pm_fragment = new PM_Fragment();
        fragmentTransaction.replace(android.R.id, pm_fragment);

    }

    fragmentTransaction.commit();


}

}

Simon Coates
Simon Coates
28,692 Points

If this is related to a challenge, you might want to post a link to the challenge in question. Otherwise, some details about the error might help people with debugging.

2 Answers

zarah hafeez
zarah hafeez
317 Points

Below are the errors:

Error:(19, 41) error: incompatible types: android.app.FragmentTransaction cannot be converted to android.support.v4.app.FragmentTransaction Error:(23, 50) error: cannot find symbol variable id Error:(27, 50) error: cannot find symbol variable id :app:compileDebugJavaWithJavac FAILED Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

Simon Coates
Simon Coates
28,692 Points

I don't know Android, but I took a look. I've seen a couple similar errors related to mixing libraries resulting from faulty import statements. The android.app.FragmentManager beginTransaction method returns a android.app.FragmentTransaction, which doesn't seem to be able to be be accepted as a android.support.v4.app.FragmentTransaction (which is what your import statement indicated is the full type of FragmentTransaction). (sorry i couldn't be more help. i don't know how these libraries are meant to relate to each other )

Kourosh Raeen
Kourosh Raeen
23,732 Points

Since you are using the Support Library version of Fragment you need to use getSupportFragmentManager() instead of getFragmentManager().