首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >androidx.fragment.app.Fragment不能转换为android.app.Fragment

androidx.fragment.app.Fragment不能转换为android.app.Fragment
EN

Stack Overflow用户
提问于 2020-12-10 09:14:33
回答 2查看 3.1K关注 0票数 2

我有个碎片文件。有一条错误消息

“错误:不兼容类型: androidx.fragment.app.Fragment不能转换为android.app.Fragment. androidx.fragment.app.Fragment片段).addToBackStack(Null);

我知道我的build.gradle文件有问题,但我不知道我应该使用哪个依赖项。我已经阅读了相关的文档和问题,但有些依赖是不可取的。这是我的MainActivity文件:

代码语言:javascript
运行
复制
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文件

代码语言:javascript
运行
复制
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'

}

谢谢

EN

Stack Overflow用户

发布于 2020-12-10 09:22:43

问题是HomeFragment是什么?它扩展了内置的Fragmentandroidx版本?它应该扩展androidx版本。然后你必须使用getSupportFragmentManager

代码语言:javascript
运行
复制
FragmentManager fragmentManager = getSupportFragmentManager();

那么您的IDE应该建议您修复导入,甚至会为您修改--不应该使用任何android.app.Fragment...导入,只应该使用androidx.fragment...行/包。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65231603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档