Android 开发基础系列——程序崩溃之Unable to instantiate activity ComponentInfo

这两天在搬运学习OpenCV的一个例程:OpenCV Tutorial 2 - Mixed Processing,复制粘贴,添加OpenCV库引用,程序很快就搞出来编译通过了,but,在运行的时候,刚点开程序就崩了。

调试信息显示:

Unable to instantiate activity ComponentInfo{com.emotiondetect/com.emotiondetect.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.emotiondetect.MainActivity" on path: /data/app/com.emotiondetect-1.apk

百度了一下,大部分都是 这样的答案,与我情况都不相符。

后来就把MainActivity中非必要的代码都删掉,果然还是报错,然后就意外地发现,原来在Eclipse中,xml文件也是有错误检测的--|,如下图所示:


终于发现问题所在,在谷歌上找到篇看起来比较靠谱的问答,摘抄如下:

An error message of:

The following classes could not be instantiated: org.opencv.android.JavaCameraView. java.lang.ClassNotFoundException: org.opencv.R$styleable

indicates that it isn't finding OpenCV resources when doing the build. If you're including OpenCV as a jar library, that will be the source of the problem -- jars don't have resources in them.

The solution is to build OpenCV as an AAR, because AARs do have resources. Construct a library module for it, compile it to AAR, and reuse that archive everywhere you need to.

确实,我在工程中只是添加了一个jar,如下图示:



然后,把jar引用删掉,直接添加对OpenCV工程的引用(当然,首先你要在Workplace中打开OpenCV Library的工程),如下2图所示:



Finally,程序如我们所愿,成功运行了,呵呵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值