Reputation: 319
I have a fragment file. There is an error message
"error: incompatible types: androidx.fragment.app.Fragment cannot be converted to android.app.Fragment. fragmentTransaction.replace(R.id.frame_layout, fragment).addToBackStack(null);"
I know there is something wrong with my build.gradle file but i don't know which dependency should i used. I already read the documentation and questions related but some dependency are deprecated. This is my MainActivity file:
package com.example.notesapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(HomeFragment.newInstance(), true);
}
@SuppressLint("ResourceType")
public void replaceFragment(Fragment fragment, Boolean istransition){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(istransition) {
fragmentTransaction.setCustomAnimations(android.R.anim.slide_out_right, android.R.anim.slide_in_left);
}
fragmentTransaction.replace(R.id.frame_layout, fragment).addToBackStack(null);
}
}
And this is my build.gradle file
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.notesapp"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//material design
implementation 'com.google.android.material:material:1.2.1'
//circle image view
implementation 'de.hdodenhof:circleimageview:3.1.0'
//scalable unit text size
implementation 'com.intuit.ssp:ssp-android:1.0.6'
//scalable unit size
implementation 'com.intuit.sdp:sdp-android:1.0.6'
//room database
implementation 'androidx.room:room-runtime:2.2.5'
annotationProcessor 'androidx.room:room-compiler:2.2.5'
implementation 'com.intuit.ssp:ssp-android:1.0.6'
}
Thanks
Upvotes: 2
Views: 5402
Reputation: 19253
the question is what is HomeFragment
- it extends built-in Fragment
or androidx
version? it should extend androidx
version. and then you have to use getSupportFragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
then your IDE should suggest you to fix imports or even would do it for you - there shouldn't be used any android.app.Fragment...
imports, only androidx.fragment...
lines/packages
Upvotes: 1
Reputation: 71
Use
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction
Instead of
import android.app.FragmentManager;
import android.app.FragmentTransaction;
Upvotes: 4