【发布时间】:2021-03-21 16:53:03
【问题描述】:
我有一个片段文件。有错误提示
“错误:不兼容的类型:androidx.fragment.app.Fragment 不能 转换为 android.app.Fragment。 fragmentTransaction.replace(R.id.frame_layout, 片段).addToBackStack(null);"
我知道我的 build.gradle 文件有问题,但我不知道应该使用哪个依赖项。我已经阅读了相关的文档和问题,但不推荐使用某些依赖项。这是我的 MainActivity 文件:
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);
}
}
这是我的 build.gradle 文件
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'
}
谢谢
【问题讨论】:
标签: android android-studio android-fragments dependencies fragment