android.content.ActivityNotFoundException on calling new Activity in Android

1.7k views Asked by At

I'm getting exception

12-12 17:31:18.401: E/AndroidRuntime(18177): FATAL EXCEPTION: main
12-12 17:31:18.401: E/AndroidRuntime(18177): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.testalias.sp/com.example.testalias.sp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1635)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.Activity.startActivityForResult(Activity.java:3430)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.Activity.startActivityForResult(Activity.java:3391)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.Activity.startActivity(Activity.java:3626)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.Activity.startActivity(Activity.java:3594)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at com.example.testalias.AliasTestActivity$2.onClick(AliasTestActivity.java:40)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.view.View.performClick(View.java:4354)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.view.View$PerformClick.run(View.java:17961)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.os.Handler.handleCallback(Handler.java:725)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.os.Looper.loop(Looper.java:137)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at android.app.ActivityThread.main(ActivityThread.java:5328)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-12 17:31:18.401: E/AndroidRuntime(18177):    at dalvik.system.NativeStart.main(Native Method)

My manifest code

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testalias.AliasTestActivity"
        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=".sp.SecondActivity"
        android:label="@string/app_name" >
    </activity>
</application>

And my code

btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent();
            intent1.setComponent(new ComponentName("com.example.testalias.sp", "com.example.testalias.sp.SecondActivity"));
            startActivity(intent1);
        }
    });

May be I have done this at least thousand times, but don't know why not able to do now. Please help me.

7

There are 7 answers

1
Hariharan On BEST ANSWER

Try this..

You can use your code same like it is. just change the package name inside your ComponentName com.example.testalias.sp to com.example.testalias and try again

btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent();
            intent1.setComponent(new ComponentName("com.example.testalias", "com.example.testalias.sp.SecondActivity"));
            startActivity(intent1);
        }
    });
0
venciallee On
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.testalias">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".AliasTestActivity"
            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=".sp.SecondActivity"
            android:label="@string/app_name" >
         </activity>
    </application>
</manifest>

then try again.

0
Sharmilee On

If you want to call SecondActivity from AliasTestActivity add following code to code to call new Activity.

Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
  startActivity(intent1);

instead of

Intent intent1 = new Intent();
            intent1.setComponent(new ComponentName("com.example.testalias.sp", "com.example.testalias.sp.SecondActivity"));
            startActivity(intent1);
0
rachit On

Use this:

  Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
  startActivity(intent1);

Instead of setting the component. why are you using component?

0
NagarjunaReddy On

try this in place of your code

Intent intent = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent);

In manifest declare like this in second activity

     <activity
        android:name=".SecondActivity"  >
     </activity>
0
Shailendra Madda On

Instead of this:

btn2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent1 = new Intent();
        intent1.setComponent(new ComponentName("com.example.testalias.sp", "com.example.testalias.sp.SecondActivity"));
        startActivity(intent1);
    }
});

Replace with:

btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent();
            Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent1);
        }
    });
0
Victor Pozdnyakov On

After 100 attempts i fixed this ActivityNotFoundException during new ComponentName() constructor calling like this:

Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(), "YOUR_ACTIVITY_NAME_IN_MANIFEST"));
context.startActivity(intent);