android.content.ActivityNotFoundException: Unable to find explicit activity class {}; have you declared this activity in your AndroidManifest.xml?

10,858

You make this a bit to complicated. If you want to start another Activity inside your project, you can use the following snippet:

final Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);

This is way shorter and much m ore important, you won't use the activity names as strings. In that way, when you refactor the activity class name in your IDE, it can also change the code. When using the string name, most of the IDEs won't change it.

Also you must make sure, that you declare the activity correct in your Manifest file. You don't need to use full class names, but if you use .in.Principal make sure the package of your Manifest file is com.stable.app.

Share:
10,858
BackToBasics
Author by

BackToBasics

Updated on June 13, 2022

Comments

  • BackToBasics
    BackToBasics almost 2 years

    This is the scenario: i have 2 diferent package in the same proyect. When i try to launch vía intent ActivityB (which is in com.stable.app.in) from package com.stable.app ActivityA, i get this error. My Manifest is OK i guess, but seems to be wrong. My Manifest is:

    <activity android:name=".in.Principal">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
    </activity>
    

    And my code is:

    final Intent intent = new Intent();                
    intent.setClassName("com.stable.app.in", "com.stable.app.in.Principal");
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(intent);
    

    I have tried a lot of suggestions seemed in other topics. Those are: Give to Manifest the whole route of the package. try to cut out MAIN, LAUNCHER, etc. a lot more.

    If you have had the same problem, or have a potencional solution, dont hasitate to respond!

  • BackToBasics
    BackToBasics almost 12 years
    Finally, i made it, this way works: final Intent intent = new Intent(getApplicationContext(),Principal.class);
  • BackToBasics
    BackToBasics almost 12 years
    Thanks to everyone, Tim Roes, you were right, i had to change string names to this: final Intent intent = new Intent(getApplicationContext(),Principal.class); also importing com.stable.app.in.Principal Still understand why doesnt it work as "strings": intent.setClassName("com.stable.app.in", "com.stable.app.in.Principal");
  • Tim Roes
    Tim Roes almost 12 years
    You should have removed the setAction() and setCategory() call. The ones you passed to the intent, are used for the Intent, that is used by the launcher, to start your application. So you shouldn't use them for internal activity switching. (Still when your manifest file was correct, it should have worked also with them.)
  • Varun Bhatia
    Varun Bhatia about 10 years
    after trying so many things, final Intent intent = new Intent(getApplicationContext(),Principal.class); worked. Thanks user1427871
  • Will
    Will over 9 years
    @user1427871 after 3 hours finding your comment helped me figure this out. Thank you!