Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package rename and error "Activity class does not exist"

I have a splash activity which starts another activity like this

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        final Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int wait = 0;
                    while (_isActive && (_splashTime > wait)) { 
                        sleep(100);

                        if (_isActive) {
                            wait += 100;
                        }
                    }
                } catch (InterruptedException e) {
                    Log.d(TAG, e.getMessage());

                } finally {
                    startActivity(new Intent("com.path1.path2.SomeActivity"));
                    finish();
                }
            }
        };
        splashThread.start();
    }

To start another activity I use string parameter for the Intent constructor. The corresponding class is paired with the splash string like this

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.path1.path2"
          android:versionCode="2"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4"/>

    <!--permissions-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:label="@string/app_name" android:icon="@drawable/icon">
        <activity android:name=".SplashActivity"
                  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=".SomeActivity"
                  android:label="@string/app_name"
                >
            <intent-filter>
                <action android:name="com.path1.path2.SomeActivity"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name=".OtherActivity" android:label="@string/app_name"
                />

        <!--services-->
        <service android:name=".AlarmService"/>

    </application>
</manifest>

This works flawlessly UNTIL I rename the package name. I rename the package name in the Manifest by using refactoring and the IDE renames all other classes accordingly. But when I want to start newly renamed project, I face an error

Launching application: com.path1.pathOLD/com.path1.path2.SplashActivity.
DEVICE SHELL COMMAND: am start -n "com.path1.pathOLD/com.path1.path2.SplashActivity"
Starting: Intent { cmp=com.path1.pathOLD/com.path1.path2.SplashActivity }
Error type 3
Error: Activity class {com.path1.pathOLD/com.path1.path2.SplashActivity} does not exist.

It seems that the app tries to start the Splash activity using OLDpath/NEWpath.Splash path and the error is there, but I can't find why it is using such path.

I use IntelliJ IDE. Any ideas? Could it be in the Filter in the 2nd activity in the Manifest?!

like image 288
sandalone Avatar asked Nov 18 '11 09:11

sandalone


5 Answers

It is a bug in Android Studio. To fix it:

  1. Close Studio.
  2. Remove .idea/workspace.xml
  3. Launch Studio.
like image 159
sasha_trn Avatar answered Nov 14 '22 06:11

sasha_trn


The error was in IntelliJ IDEA after all. When you create a project, the Configuration checks Launch feature automatically and prints the name of default class. When you change the name of package, refactoring does not change the configuration string which still points to the old class name. That is why there was not compile-time error, but runtime error.

It would be great if they could fix this issue in this awesome IDE as these kind of errors are very hard to track down (this one took 4 months to realize where the error was).

like image 45
sandalone Avatar answered Nov 14 '22 06:11

sandalone


File -> Invalidate caches/Restart may fix this type of errors

like image 32
Arjun Avatar answered Nov 14 '22 06:11

Arjun


Did you notice this difference (For package name in manifest),

The Launching Activity is com.path1.pathOLD/com.path1.path2.SplashActivity.

Change package in manifest file .. use this modified manifest and let me know what happen..

EDIT: Modified Manifest file,

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.path1.path2"
      android:versionCode="2"
      android:versionName="1.0">
<uses-sdk android:minSdkVersion="4"/>

<!--permissions-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".SplashActivity"
              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=".SomeActivity"
              android:label="@string/app_name"
            >
        <intent-filter>
           <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    <activity android:name=".OtherActivity" android:label="@string/app_name"
            />

    <!--services-->
    <service android:name=".AlarmService"/>

</application>

like image 4
user370305 Avatar answered Nov 14 '22 08:11

user370305


In my case I followed all the answers from here, but also I had to clean the cache. I followed this steps:

1. Go to ~/.gradle/ and delete caches folder
   rm -r caches
2. "Sync project with Gradle files" icon on Android Studio
3.  Run project
like image 3
Jorge Casariego Avatar answered Nov 14 '22 06:11

Jorge Casariego