'Cannot resolve method getSupportFragmentManager()
I found the message Cannot resolve method 'getSupportFragmentManager ( )' I want to add a fragment in an activity
here is my code
package com.formation.testfragment;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.*;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
}
}
here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.formation.testfragment"
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
mavenCentral()
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.android.support:support-v4:27.1.1'
}
but the method getSupportFragmentManager ( ) not found. Thank you.
Solution 1:[1]
getSupportFragmentManager() is a method on FragmentActivity. You need to inherit from it — either directly or indirectly (e.g., by extending AppCompatActivity).
In your case, given your dependencies, you could extend FragmentActivity directly:
public class MainActivity extends FragmentActivity
Solution 2:[2]
You can found from the FragmentActivity documentation that getSupportFragmentManager is a method of FragmentActivity. Usually, current Android project is using support appcompat library where you have AppCompatActivity. AppCompatActivity is derived from FragmentActivity. So, you can use either FragmentActivity:
public class MainActivity extends FragmentActivity
or using AppCompatActivity:
public class MainActivity extends AppCompatActivity
Solution 3:[3]
getSupportFragmentManager() is 1 of the method from FragmentActivity
try this
public class MainActivity extends FragmentActivity
The use of the method getSupportFragmentManager()
Normally people will do like this, to switch the fragment
read more about fragment activity Here is the Link to read more about fragment
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder, new FooFragment());
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commit();
R.id.frameLayout is the id from <frameLayout> in your Layout xml file.
Example:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout">
</FrameLayout>
Solution 4:[4]
You just need to extend your class to AppCompatActivity i.e,public class MainActivity extends Activity to public class MainActivity extends AppCompatActivity
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CommonsWare |
| Solution 2 | ישו ×והב ×ותך |
| Solution 3 | Kopi Bryant |
| Solution 4 | Prashant |
