gpt4 book ai didi

android - 引用模块中的资源

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:15 24 4
gpt4 key购买 nike

我在 Android Studio 中创建了一个模块。在模块代码中,我想显示一个使用模块中定义的布局的对话框。当我引用像 net.gwtr.module.R.layout.my_dialog_layout 这样的布局时,我得到这个异常;

java.lang.ClassNotFoundException: Didn't find class "net.gwtr.module.R$layout" on path: DexPathList[[zip file "/data/app/net.gwtr.moduletest-1.apk"],nativeLibraryDirectories=[/data/app-lib/net.gwtr.moduletest-1, /vendor/lib, /system/lib]]

我认为原因是当你向项目添加模块时资源被合并了。它没有为模块包名称创建不同的资源 ID。所以我无法访问模块包中的资源。

如何在模块代码中引用模块资源?

编辑1:我这样引用资源;

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.show();

编辑2:我找到了一种方法,但我不想每次引用资源时都使用它。

当我用这个获取资源 ID 时,我可以访问资源;

context.getResources().getIdentifier("my_dialog_layout", "layout", context.getPackageName())

最佳答案

在模块的 gradle 文件中。

你是否编译了你的其他模块?

compile project(':othermodule')

关于android - 引用模块中的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25421572/

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