Search code examples
androidandroid-library

Unable to find explicit activity class exception even if the class is declared in the projects manifest


I transformed my project into a library. This project just displays a blank activity. I imported this library into another project where I want to call it thru the projects main activity but I keep on getting the error below even if I already declared the activity inside the projects manifest.

Unable to find explicit activity class {com.package.name/com.package.name.BatibotActivity}; have you declared this activity in your AndroidManifest.xml?

This is my project manifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="pak.paks.pocket" >
         <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" >
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.package.name.BatibotActivity"
                android:screenOrientation="portrait" >
            </activity>

        </application>
</manifest>

What am I doing wrong?

edit:

this is my BatibotActivity

package com.package.name;

import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;

public class BatibotActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_batibot);
    }
}

EDIT2 This is the class that is calling the BatibotActivity

package pak.paks.pocket;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void submitBtn(View view) {

        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        intent.setComponent(new ComponentName("com.package.name",
                "com.package.name.BatibotActivity"));

        startActivity(intent);

    }
}

Solution

  • By the log message, you are trying to open com.package.name/com.package.name.BatibotActivity, but the Activity registered on AndroidManifest.xml is pak.paks.pocket/com.package.name.BatibotActivity.

    The first parameter for ComponentName is the package name. This is the correct code:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    intent.setComponent(new ComponentName("pak.paks.pocket", "com.package.name.BatibotActivity"));
    startActivity(intent);