简体   繁体   English

创建android外部jar作为库,找不到资源

[英]Create android external jar as library, no find resources

I got a problem when I tried to do this: 尝试执行此操作时遇到问题:

LinearLayout container = (LinearLayout)findViewById(R.id.chat_list_container);

android.content.res.Resources$NotFoundException: Resource ID #0x7f030001 android.content.res.Resources $ NotFoundException:资源ID#0x7f030001

This is a code from external jar I have just created and added to my application. 这是我刚创建并添加到我的应用程序中的外部jar中的代码。

By the way this exact code working when I used it as import module and not use it like external jar. 顺便说一句,当我将其用作导入模块并且不像外部jar一样使用时,此确切的代码有效。

Any help? 有什么帮助吗?

Thanks! 谢谢!

Android will not look for resources in any external jar because they are expected to contains just executable code. Android不会在任何外部jar中寻找资源,因为它们仅包含可执行代码。

If you want to publish resources as library as well, things gets more ugly. 如果您还希望将资源发布为库,则情况将变得更加难看。 In this case, you could create a resource apk just like framework-res.apk. 在这种情况下,您可以像framework-res.apk一样创建资源apk。 To do that, create an android project with no source, place the res files and add this to your AndroidManifest.xml: 为此,请创建一个没有源代码的android项目,放置res文件并将其添加到您的AndroidManifest.xml中:

...
<application
    ...
    android:hasCode=false >
    ...
</application>

In this case, you will have to retrieve the res id directly from the res-apk: 在这种情况下,您将必须直接从res-apk中检索res ID:

int resId = getPackageManager().getResourcesForApplication("res.apk.package.name")
.getIdentifier("chat_list_container" , "layout", "res.apk.package.name");

// Maybe getResources.getIdentifier() works, but I'm not sure.
LinearLayout container = (LinearLayout)findViewById(resId);

I should mention that the device which will run the application must also has the resource apk installed. 我应该提到,将运行该应用程序的设备还必须安装了资源apk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM