Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

java - How to call activity from a library module in android studio

I am trying to call an activity from a library module in my application. I keep getting the error

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?

I am calling the activity as

Intent intentGIS = new Intent(android.content.Intent.ACTION_VIEW);
intentGIS.setComponent(new ComponentName("com.nextgis.mobile",
                    "com.nextgis.mobile.activity.MainActivity"));
startActivity(intentGIS);

App manifest :

<?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>

Library 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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use reflection to get class object.

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" is the Activity present in Main project, that we need to call from its Library project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...