迁移至AndroidX后ResolveDialogFragment的show方法无法解析求助
解决AndroidX迁移后ResolveDialogFragment的show方法无法解析问题
问题场景
运行他人开发的Android项目时,原依赖使用旧版support库:
implementation 'com.android.support:appcompat-v7:23.3.0'
收到IDE提示:
Version 28 (intended for Android Pie and below) is the last version of
the legacy support library, so we recommend that you migrate to
AndroidX libraries when using Android Q and moving forward. The IDE
can help with this: Refactor > Migrate to AndroidX...
手动替换为AndroidX依赖:
implementation 'androidx.appcompat:appcompat:1.3.1'
并在Java文件中更新导入:
import androidx.appcompat.app.AppCompatActivity;
之后出现编译错误:
"Cannot resolve method 'show' in 'ResolveDialogFragment'"
相关代码片段
MainActivity中调用Dialog的关键代码
Button resolveButton = findViewById(R.id.resolve_button); resolveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cloudAnchor != null) { snackbarHelper.showMessageWithDismiss(getParent(), "Please clear anchor."); return; } ResolveDialogFragment dialog = new ResolveDialogFragment(); dialog.setOkListener(MainActivity.this::onResolveOKPressed); dialog.show(getSupportFragmentManager(), "Resolve"); // 此处报错 } });
当前Gradle依赖
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test.ext:junit:1.1.3' implementation 'com.google.android.material:material:1.0.0-alpha3' implementation 'com.google.firebase:firebase-database:20.2.0' implementation "com.google.ar.sceneform.ux:sceneform-ux:1.4.0" }
解决方法
1. 修复ResolveDialogFragment的AndroidX适配
ResolveDialogFragment大概率继承了旧版support库的DialogFragment,需同步迁移到AndroidX:
- 打开
ResolveDialogFragment类文件,将导入替换为:import androidx.fragment.app.DialogFragment; - 确保类继承的是AndroidX版本的
DialogFragment:public class ResolveDialogFragment extends DialogFragment { // ... 原有代码 }
2. 完成全项目AndroidX迁移
手动替换依赖容易遗漏,建议使用IDE自动迁移工具:
- 点击菜单栏的
Refactor->Migrate to AndroidX - 按照向导完成迁移,IDE会自动替换所有support库依赖、导入语句及相关代码
3. 补充AndroidX Fragment依赖(可选)
如果迁移后仍有Fragment相关报错,可在Gradle中添加AndroidX Fragment依赖:
implementation 'androidx.fragment:fragment:1.4.1' // 版本可与appcompat匹配
内容的提问来源于stack exchange,提问作者rakshitha Poojary
相关产品推荐
相关产品推荐

