在另一个项目中使用 Android 库项目 Activity
我有一个 Android 库项目,我想在另一个 Android 项目中使用它。
该库在其 AndroidManifest 中声明了一个 Activity。当我在第二个项目中尝试以下操作时:
Intent intent = new Intent(this, ReaderActivity.class);
startActivity(intent);
出现以下异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.digitalpages.reader.demo/br.com.digitalpages.reader.demo.ReaderDemoActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
...
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
...
如何从另一个项目打开活动?
编辑: 根据用户的回答,我将以下行添加到我的第二个项目中,
<uses-library android:name="br.com.digitalpages.reader" android:required="true" />
但它仍然不起作用
I have an Android library project that I would like to use from within another Android project.
The library has a Activity declared in its AndroidManifest. When I try the following within the second project:
Intent intent = new Intent(this, ReaderActivity.class);
startActivity(intent);
I get the following exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.digitalpages.reader.demo/br.com.digitalpages.reader.demo.ReaderDemoActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
...
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
...
How can I open the Activity from the another project?
EDIT:
By users answers I added the following line into my second project
<uses-library android:name="br.com.digitalpages.reader" android:required="true" />
But it's still doesn't works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我相信您必须将在你自己的 AndroidManifest.xml 中——我不认为它是从库中获取的。我没有方便的参考资料。
更新:这是官方解决方案。来自文档:
I believe you must include the <activity> in your own AndroidManifest.xml -- I don't think it gets picked up from a library. I don't have my reference for that handy.
Update: It's official solution. From the doc:
并确保您已在图书馆声明了这些活动。您不需要在当前项目的清单中声明库活动。
And make sure in library you have declared the activities. You don't need to declare the library activities in your current project's manifest.
你添加到清单了吗?
http://developer.android.com/guide/topics/manifest /uses-library-element.html
did you add to the manifest?
http://developer.android.com/guide/topics/manifest/uses-library-element.html
这有效:
在您的库中,放入您的自定义
Activity
:无需将其放入清单中。
在您的调用 Android 项目中,创建一个空(虚拟)包装器:
并将对该类的引用添加到您的清单中:
This works:
In your library, put your custom
Activity
:No need to put it into a manifest.
In your calling Android project, create an empty (dummy) wrapper:
and add reference to this class to your manifest:
我知道这个问题已经很老了,但我认为这可能会帮助像我这样遇到同样问题的人。
使用 Eclipse,在库之间共享代码和活动的最佳方法可能是可以在此处的 android 文档中找到的方法:
使用 ADT 从 Eclipse 管理项目
I am aware that the question is quite old but I think this might help people like me that came up with the same problem.
Using Eclipse, the best way to share code and activities among libraries is probably the one that can be found in the android documentation here:
Managing Projects from Eclipse with ADT
当在库内使用活动时,应仅在库的清单内声明该活动。
该活动可以从主应用程序启动,如下所示:
我尝试像以下代码一样启动库的活动,但我警告您:它不起作用
When using an activity inside the library, the activity should be declared only inside the manifest of library.
The activity can be started from the main app like this:
I tried to start the library's activity like the following code, but i warn you: it doesn't work
您应该只导入活动的代码(也不是清单),然后在第二个项目的清单中声明您的活动(库的)。
you should import just the code of the activity ( not the manifest too ) , and then , declare your Activity ( of the library ) , in the manifest of your second project
如果您已在启动库的活动时使用以下代码将活动添加到库的清单中,则无需在主项目的清单中显式添加活动。
对于 Kotlin
对于 Java
You don't need to explicitly add activity in your main project's manifest if you have already added the activity in your library's manifest by using the following code when starting library's activity.
For Kotlin
For Java