androidx.fragment.app.Fragment 无法转换为 android.app.Fragment
androidx.fragment.app.Fragment cannot be converted to android.app.Fragment
我有一个片段文件。有一条错误信息
"error: incompatible types: androidx.fragment.app.Fragment cannot be
converted to android.app.Fragment.
fragmentTransaction.replace(R.id.frame_layout,
fragment).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'
}
谢谢
使用
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction
而不是
import android.app.FragmentManager;
import android.app.FragmentTransaction;
问题是什么是 HomeFragment
- 它扩展了内置的 Fragment
或 androidx
版本?它应该扩展 androidx
版本。然后你必须使用 getSupportFragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
那么你的 IDE 应该建议你修复导入,甚至会为你做 - 不应该使用任何 android.app.Fragment...
导入,只有 androidx.fragment...
lines/packages
我有一个片段文件。有一条错误信息
"error: incompatible types: androidx.fragment.app.Fragment cannot be converted to android.app.Fragment. fragmentTransaction.replace(R.id.frame_layout, fragment).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'
}
谢谢
使用
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction
而不是
import android.app.FragmentManager;
import android.app.FragmentTransaction;
问题是什么是 HomeFragment
- 它扩展了内置的 Fragment
或 androidx
版本?它应该扩展 androidx
版本。然后你必须使用 getSupportFragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
那么你的 IDE 应该建议你修复导入,甚至会为你做 - 不应该使用任何 android.app.Fragment...
导入,只有 androidx.fragment...
lines/packages