如何从android studio中的库模块调用活动

时间:2017-01-04 13:30:45

标签: java android android-studio module android-library

我正在尝试从我的应用程序中的库模块调用一个活动。我一直收到错误

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.nextgis.mobile/com.nextgis.mobile.activity.MainActivity}; have you declared this activity in your 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), bishop.dev@gmail.com
~ Author:   NikitaFeodonit, nfeodonit@yandex.com
~ Author:   Stanislav Petriakov, becomeglory@gmail.com
~      ******************************************************************************
~ Copyright (c) 2012-2016 NextGIS, info@nextgis.com
~
~ 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>

我已在模块compile project(':gisapp')的gradle文件中添加了app。我知道gradle会自动显示合并,但我可能错了。

我在这里做错了什么?

注意:This是我用作库的项目。

1 个答案:

答案 0 :(得分:14)

我们可以使用反射来获取类对象。

的Class.forName( “com.mypackage.myMainActivity”)

在Library项目中添加此代码以进行调用,

try {
      Intent myIntent = new Intent(this,Class.forName("com.mypackage.myMainActivity"));
      startActivity(myIntent );
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

“com.mypackage.myMainActivity”是Main项目中的Activity,我们需要从它的Library项目中调用它。