studio中的库模块调用活动

studio中的库模块调用活动

本文介绍了如何从android studio中的库模块调用活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的应用程序中的库模块调用活动.我不断收到错误

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>

我在模块 app 的 gradle 文件中添加了 compile project(':gisapp').我知道 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.

我在这里做错了什么?

注意:这个是我用作库的项目.

Note : This is the project which I am using as library.

推荐答案

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

We can use reflection to get class object.

Class.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" 是 Main 项目中的 Activity,我们需要从它的 Library 项目中调用.

"com.mypackage.myMainActivity" is the Activity present in Main project, that we need to call from its Library project.

这篇关于如何从android studio中的库模块调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:02