Unity安卓报错解决方法:android.content.res.Resources$NotFoundException

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wolf96/article/details/79021303
参考:
原理是这样的

Important change: We have changed the way Library Projects generate and package R classes:
The R class is not packaged in the jar output of Library Projects anymore.
Library Project do not generate the R class for Library Projects they depend on. 
Only main application projects generates the Library R classes alongside their own.



解决方法是这样的
Don't load resources based on their compile-time ID; use Activity.getResources().getIdentifier() to retrieve the ID instead.

之前这句话报错
setContentView(R.layout.*****);
改成这样完美解决
int layoutId = getResources().getIdentifier( " ***** " , "layout" , getPackageName());
System. out .println( "layOutid为++" + layoutId );
setContentView( layoutId );

总结
简单来说是因为eclipse生成的R不是Unity的R,里面ID对应不上,所以找不到资源
反编译了一下 setContentView(R.layout.*****); 生成的jar包,里面的结果是直接把资源ID写死进去
setContentView(23423455);
这样Unity的R肯定找不到eclipse里定义的资源

int layoutId = getResources().getIdentifier( " ***** " , "layout" , getPackageName());
setContentView( layoutId );
这样做了的话,资源id是运行时动态获取的,所以肯定没问题

猜你喜欢

转载自blog.csdn.net/wolf96/article/details/79021303