当使用 Android ActivityResult API 时,可能会遇到 "registerForActivityResult" 的未解决引用错误。这是因为 ActivityResult API 是在 AndroidX 库中引入的,而不是在旧的支持库中。
要解决此错误,您需要进行以下步骤:
gradle.properties 文件中添加以下行来实现迁移:android.useAndroidX=true
android.enableJetifier=true
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
private ActivityResultLauncher mGetContentLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback() {
@Override
public void onActivityResult(Uri result) {
// 在此处处理活动结果
}
});
mGetContentLauncher.launch() 方法。例如,如果您想要选择一个图像,可以使用以下代码:mGetContentLauncher.launch("image/*");
确保在调用 launch() 方法时传递合适的参数,以便匹配您注册的活动结果合同。
通过执行上述步骤,您将能够解决 "registerForActivityResult" 的未解决引用错误,并正确使用 Android ActivityResult API。
上一篇:Android Activity按钮的onClick()监听器的工作速度较慢
下一篇:android ActivityResultContracts.OpenDocumentTree() Intent总是打开上次使用的最后一个文件夹。