Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialzarah hafeez
317 PointsGetting 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();
}
}
2 Answers
zarah hafeez
317 PointsBelow 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
28,694 PointsI 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
23,733 PointsSince you are using the Support Library version of Fragment you need to use getSupportFragmentManager() instead of getFragmentManager().
Simon Coates
28,694 PointsSimon Coates
28,694 PointsIf 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.