2

I keep getting "ActivityNotFoundException: Unable to find explicit activity class ... " when using Eclipse's emulator to call the activity of another application from an application. Perhaps the problem maybe related to I am not able to download to/find both applications at the same time when I click on "Manage Applications" in Settings. This is the first project I need to call the activity of another application. But I am not sure the code is correct either. Please help me determine if there are errors in the code snippets I present below. It is hinted that I can set the action field of the intent to achieve the objective but have not found learning material for this. I learned about using the setComponent method in the calling app and to add android:export to the called activity's AndroidManifest.xml. Thanks in advance!

Calling app's relevant source code:

Intent intent = new Intent();                
intent.setComponent(new ComponentName("com.MyPackage", om.MyPackage.Activity1));
startActivity(intent);

Calling app's relevant AndroidManifest.xml :

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Activity1">
        <intent-filter>
            <action android:name="com.MyPackage.Activity1" />
            <category android:name="android.intent.category.DEFAULT" />               
        </intent-filter>
    </activity>

</application>

Relevant code of AndroidManifest.xml of the activity of another application

<activity android:name=".Activity1" android:exported = "true">          
        <intent-filter>
            <action android:name="com.MyPackage.Activity1" />
            <category android:name="android.intent.category.DEFAULT" />             
        </intent-filter>
</activity>
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
macrogeo
  • 97
  • 1
  • 11

1 Answers1

4

Firstly point out that you're trying to start Activity in Application2 from Activity in Application1

  • You have to give them separate namespaces
    • both applications now have com.MyPackage.* prefix
  • OR use names Activity1 and Activity2

So you will have

com.MyPackage1.Activity1
// and
com.MyPackage2.Activity1

Then you can use this code, to start Activity1 in MyPackage2 from MyPackage1.

// in file com.MyPackage1.Activity1
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.MyPackage2", "com.MyPackage2.Activity1"));
startActivity(intent);

And your AndroidManifest.xml files should look like this:

first

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.MyPackage1.Activity1"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

second

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.MyPackage2.Activity1"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

see related SO question:
How to start activity in another application?

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • I am still getting ActivityNotFoundException or ClassNotFoundException after making the recommended changes. Perhaps it has something to do with not able to install the apps on the emulator correctly. In my latest effort, First I installed the called app (which contains Activity1 class) using adb command. Then I run the calling app from Eclipse. This generates ClassNotFoundException regarding Activity1, which is probably caused by the called app no longer in the emulator for some strange reason. Why is it no longer installed? How to solve it if this is part of my problem? Thanks! – macrogeo Mar 31 '12 at 08:19
  • You're trying to call Activity in another application? I was pointing at starting second activity in the same Application. See my edited answer – Marek Sebera Mar 31 '12 at 12:05