问题描述
我正在尝试从我的应用程序中的库模块调用一个活动。我一直收到错误
I am trying to call an activity from a library module in my application. I keep getting the error
android.content.ActivityNotFoundException:无法找到显式活动类{com.nextgis.mobile/com.nextgis.mobile .activity.MainActivity};你有没有在AndroidManifest.xml中声明这个活动?
我将活动称为
Intent intentGIS = new Intent(android.content.Intent.ACTION_VIEW);
intentGIS.setComponent(new ComponentName("com.nextgis.mobile",
"com.nextgis.mobile.activity.MainActivity"));
startActivity(intentGIS);
应用清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.disarm.sanna.pdm">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.READ_SYNC_STATS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<!--
<uses-permission android:name="android.permission.CAMERA"/>
-->
<uses-feature
android:name="android.hardware.location"
android:required="true"
/>
<uses-feature
android:name="android.hardware.telephony"
android:required="false"
/>
<uses-feature
android:name="android.hardware.wifi"
android:required="false"
/>
<!--
<uses-feature
android:name="android.hardware.camera"
android:required="false"
/>
<uses-feature
android:name="android.hardware.compass"
android:required="false"
/>
-->
<supports-screens
android:largeScreens="true"
android:smallScreens="true"
android:anyDensity="true"
android:normalScreens="true"
android:xlargeScreens="true"
android:resizeable="true"
/>
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:node="replace">
<activity
android:name=".SplashActivity"
android:windowSoftInputMode="stateHidden|adjustUnspecified"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.disarm.sanna.pdm.ShareActivity"
android:label="@string/app_name">
</activity>
<activity android:name=".SelectCategoryActivity"
android:label="@string/app_name">
</activity>
<activity android:name=".SocialShareActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".ActivityList"
android:windowSoftInputMode="stateHidden|adjustPan"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
</activity>
<activity
android:name=".Capture.Photo"
android:theme="@style/AppCompatAlertDialogStyle"
>
</activity>
<activity
android:name=".Capture.Video"
android:theme="@style/AppCompatAlertDialogStyle"
>
</activity>
<activity
android:label="Record Audio"
android:name=".Capture.AudioCapture"
android:theme="@style/Theme.AppCompat.Light.Dialog"
>
</activity>
<activity
android:label="@string/allow_setting"
android:name=".WriteSettingActivity"
android:theme="@style/AppTheme"
>
</activity>
<service
android:name=".Service.SyncService"
android:enabled="true"
android:exported="true" />
<service android:name=".DisarmConnect.MyService" />
</application>
</manifest>
图书馆清单:
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Project: NextGIS Mobile
~ Purpose: Mobile GIS for Android.
~ Author: Dmitry Baryshnikov (aka Bishop), [email protected]
~ Author: NikitaFeodonit, [email protected]
~ Author: Stanislav Petriakov, [email protected]
~ ******************************************************************************
~ Copyright (c) 2012-2016 NextGIS, [email protected]
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nextgis.mobile"
>
<application
android:allowBackup="true"
android:label="@string/app_name"
>
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
>
</activity>
<activity
android:name=".activity.SettingsActivity"
android:label="@string/action_settings"
/>
<activity
android:name=".activity.NGWSettingsActivityProxy"
android:label="@string/ngw_settings"
/>
<activity
android:name=".activity.AboutActivity"
android:label="@string/action_help"
/>
<activity
android:name=".activity.CreateVectorLayerActivity"
android:label="@string/create_layer"
android:screenOrientation="portrait"
/>
<service
android:name="com.nextgis.mobile.datasource.SyncService"
>
<intent-filter>
<action
android:name="android.content.SyncAdapter"
/>
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter"
/>
</service>
<provider
android:name="com.nextgis.maplib.datasource.LayerContentProvider"
android:authorities="com.nextgis.mobile.provider"
android:exported="false"
android:syncable="true"
/>
</application>
</manifest>
我添加了编译项目(':gisapp')
在模块 app
的gradle文件中。我知道gradle会自动显示合并,但我可能错了。
I have added compile project(':gisapp')
in the gradle file for the module app
. I know gradle does manifest merging automatically but I could be wrong.
我在这里做错了什么?
What am I doing wrong here ?
注意:是哪个项目我用作图书馆。
Note : This is the project which I am using as library.
推荐答案
我们可以使用反射来获取类对象。
We can use reflection to get class object.
类.forName(com.mypackage.myMainActivity)
Class.forName("com.mypackage.myMainActivity")
在要调用的库项目中添加此代码,
Add this code in Library project to call,
try {
Intent myIntent = new Intent(this,Class.forName("com.mypackage.myMainActivity"));
startActivity(myIntent );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
com.mypackage.myMainActivity是主项目中的活动,我们需要从它的图书馆项目打电话。
"com.mypackage.myMainActivity" is the Activity present in Main project, that we need to call from its Library project.
这篇关于如何从android studio中的库模块调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!