gpt4 book ai didi

androidx.fragment.app.Fragment 无法转换为 android.app.Fragment

转载 作者:行者123 更新时间:2023-12-05 00:15:38 27 4
gpt4 key购买 nike

我有一个 fragment 文件。有错误消息

"error: incompatible types: androidx.fragment.app.Fragment cannot beconverted 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;

关于androidx.fragment.app.Fragment 无法转换为 android.app.Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65231603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com