-1

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.

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111

7 Answers7

1

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>
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
1

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);
        }
    });
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • thank you very much. I thought the first parameter should be package name of Activity. Actually the android documentation of this method is in too much high level to understand for a person whose mother tongue isn't English. – Shirish Herwade Dec 12 '13 at 14:36
0
<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.

venciallee
  • 775
  • 4
  • 19
0

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);
Sharmilee
  • 1,286
  • 1
  • 12
  • 20
0

Use this:

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

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

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
rachit
  • 1,981
  • 15
  • 23
0

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);
        }
    });
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

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);