Usar una actividad de proyecto de biblioteca de Android dentro de otro proyecto
Tengo un proyecto de biblioteca de Android que me gustaría usar desde dentro de otro proyecto de Android.
La biblioteca tiene una actividad declarada en su AndroidManifest. Cuando intento lo siguiente dentro del segundo proyecto:
Intent intent = new Intent(this, ReaderActivity.class);
startActivity(intent);
Obtengo la siguiente excepción:
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)
...
¿Cómo puedo abrir la Actividad desde otro proyecto?
EDITAR: Por usuarios respuestas He añadido la siguiente línea en mi segundo proyecto
<uses-library android:name="br.com.digitalpages.reader" android:required="true" />
Pero todavía no funciona
7 answers
Creo que debes incluir la
Actualización: Es la solución oficial. De el doc :
Declarar componentes de biblioteca en el archivo de manifiesto
En el archivo de manifiesto del proyecto de la aplicación, debe agregar declaraciones de todos los componentes que utilizará la aplicación que son importar de un proyecto de biblioteca. Por ejemplo, debe declarar cualquier
<activity>
,<service>
,<receiver>
,<provider>
, y así sucesivamente, así como<permission>
,<uses-library>
, y elementos similares.Las declaraciones deben hacer referencia a los componentes de la biblioteca nombres de bultos plenamente cualificados, cuando proceda.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-18 09:19:38
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello",
"classname//ex-com.hello.ExampleActivity"));
startActivity(intent);
Y asegúrese de que en la biblioteca ha declarado las actividades. No necesitas declarar las actividades de la biblioteca en el manifiesto de tu proyecto actual.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-31 19:44:07
Esto funciona:
En su biblioteca, ponga su costumbre Activity
:
public class MyLibraryActivity extends ListActivity { ... }
No hay necesidad de ponerlo en un manifiesto. En tu proyecto calling Android, crea un envoltorio vacío (ficticio):
public class MyActivity extends MyLibraryActivity { }
Y agrega referencia a esta clase a tu manifiesto:
<activity android:name="my_package.MyActivity" ... />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-25 21:01:11
¿Agregaste al manifiesto?
Http://developer.android.com/guide/topics/manifest/uses-library-element.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-27 21:00:25
Debe importar solo el código de la actividad ( no el manifiesto también ) , y luego , declarar su Actividad ( de la biblioteca ) , en el manifiesto de su segundo proyecto
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-27 21:02:28
Soy consciente de que la pregunta es bastante antigua, pero creo que esto podría ayudar a las personas como yo que se les ocurrió el mismo problema.
Usando Eclipse, la mejor manera de compartir código y actividades entre bibliotecas es probablemente la que se puede encontrar en la documentación de Android aquí:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-25 16:57:27
No necesita agregar explícitamente actividad en el manifiesto de su proyecto principal si ya ha agregado la actividad en el manifiesto de su biblioteca utilizando el siguiente código al iniciar la actividad de la biblioteca.
Para 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)
Para 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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-24 10:33:12