在另一个项目中使用 Android 库项目 Activity

发布于 2024-11-10 07:49:58 字数 1329 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

九公里浅绿 2024-11-17 07:49:58

我相信您必须将在你自己的 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:

Declaring library components in the manifest file

In the manifest file of the application project, you must add
declarations of all components that the application will use that are
imported from a library project. For example, you must declare any
<activity>, <service>, <receiver>, <provider>, and so on, as well as
<permission>, <uses-library>, and similar elements.

Declarations should reference the library components by their
fully-qualified package names, where appropriate.

不必了 2024-11-17 07:49:58
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello", 
                                     "classname//ex-com.hello.ExampleActivity"));
startActivity(intent);

并确保您已在图书馆声明了这些活动。您不需要在当前项目的清单中声明库活动。

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello", 
                                     "classname//ex-com.hello.ExampleActivity"));
startActivity(intent);

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.

听你说爱我 2024-11-17 07:49:58

这有效:

在您的库中,放入您的自定义Activity

public class MyLibraryActivity extends ListActivity { ... }

无需将其放入清单中。
在您的调用 Android 项目中,创建一个空(虚拟)包装器:

public class MyActivity extends MyLibraryActivity { } 

并将对该类的引用添加到您的清单中:

<activity android:name="my_package.MyActivity" ... />

This works:

In your library, put your custom Activity:

public class MyLibraryActivity extends ListActivity { ... }

No need to put it into a manifest.
In your calling Android project, create an empty (dummy) wrapper:

public class MyActivity extends MyLibraryActivity { } 

and add reference to this class to your manifest:

<activity android:name="my_package.MyActivity" ... />
慕巷 2024-11-17 07:49:58

我知道这个问题已经很老了,但我认为这可能会帮助像我这样遇到同样问题的人。

使用 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

胡渣熟男 2024-11-17 07:49:58

当在库内使用活动时,应仅在库的清单内声明该活动。
该活动可以从主应用程序启动,如下所示:

  Intent intent = new Intent();
        intent.setClassName(this, "com.duna.remotelocklibrary.activities.MainRemoteActivity");
        startActivity(intent);

我尝试像以下代码一样启动库的活动,但我警告您:它不起作用

  Intent intent = new Intent();
        intent.setClassName("com.duna.remotelocklibrary", "com.duna.remotelocklibrary.activities.MainRemoteActivity");
        startActivity(intent);

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:

  Intent intent = new Intent();
        intent.setClassName(this, "com.duna.remotelocklibrary.activities.MainRemoteActivity");
        startActivity(intent);

I tried to start the library's activity like the following code, but i warn you: it doesn't work

  Intent intent = new Intent();
        intent.setClassName("com.duna.remotelocklibrary", "com.duna.remotelocklibrary.activities.MainRemoteActivity");
        startActivity(intent);
何以心动 2024-11-17 07:49:58

您应该只导入活动的代码(也不是清单),然后在第二个项目的清单中声明您的活动(库的)。

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

习惯成性 2024-11-17 07:49:58

如果您已在启动库的活动时使用以下代码将活动添加到库的清单中,则无需在主项目的清单中显式添加活动。

对于 Kotlin

   val myIntent = Intent(activityContext, ActivityToLaunch::class.java)
    // Needed to set component to remove explicit activity entry in application's manifest
    myIntent.component = ComponentName(activityContext, PickerActivity::class.java)
    activityContext.startActivity(myIntent, PICKER_REQUEST_CODE)

对于 Java

    Intent myIntent = new Intent(activityContext, PickerActivity.class);
    // Needed to set component to remove explicit activity entry in application's manifest
    final ComponentName component = new ComponentName(activityContext, PickerActivity.class);
    myIntent.setComponent(component);
    activityContext.startActivity(myIntent, REQUEST_CODE);

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

   val myIntent = Intent(activityContext, ActivityToLaunch::class.java)
    // Needed to set component to remove explicit activity entry in application's manifest
    myIntent.component = ComponentName(activityContext, PickerActivity::class.java)
    activityContext.startActivity(myIntent, PICKER_REQUEST_CODE)

For Java

    Intent myIntent = new Intent(activityContext, PickerActivity.class);
    // Needed to set component to remove explicit activity entry in application's manifest
    final ComponentName component = new ComponentName(activityContext, PickerActivity.class);
    myIntent.setComponent(component);
    activityContext.startActivity(myIntent, REQUEST_CODE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文