casolorz
casolorz

Reputation: 9574

Crashes when opening activity through alias on manifest

I have an app that is a specialized web browser. A lot of users have asked for it to be listed as a browser on Android however I think that is pretty intrusive since it isn't a general purpose browser. So instead what I did is I created a component using an activity-alias and made it disabled by default and then the users can choose whether to enable it. This is what my alias looks like:

<activity-alias
            android:name="MyComponentName"
            android:enabled="false"
            android:exported="true"
            android:targetActivity=".MyMainActivity">
            <intent-filter android:label="@string/app_name">
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"></category>

                <data android:scheme="http"/>
                <data android:scheme="https"/>
            </intent-filter>
        </activity-alias>

Anyways, I've tested it on several Android versions and it works fine but I've gotten a few crashes over Crashlytics that look like this:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.MyMainActivity}: java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/MyComponentName}
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
       at android.app.ActivityThread.access$800(ActivityThread.java:151)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5349)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/MyComponentName}
       at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:284)
       at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:152)
       at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)
       at mypackage.MyMainActivity.onCreate(MyMainActivity.java:330)
       at android.app.Activity.performCreate(Activity.java:6020)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
       at android.app.ActivityThread.access$800(ActivityThread.java:151)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5349)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/MyComponentName}
       at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:314)
       at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:301)
       at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:281)
       at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:152)
       at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)
       at mypackage.MyMainActivity.onCreate(MyMainActivity.java:330)
       at android.app.Activity.performCreate(Activity.java:6020)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
       at android.app.ActivityThread.access$800(ActivityThread.java:151)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5349)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

Any idea what I might be doing wrong? the line it is crashing on in my code is super.onCreate() on the MyMainActivity.

Thanks.

EDIT: Manifest section for MyMainActivity Second edit: changed the android:name="MyMainActivity" to have a . before MyMainActivity but it didn't solve the issue.

<activity
            android:name=".MyMainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
           >

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

.... a bunch of other filters

</activity>

Upvotes: 3

Views: 1392

Answers (3)

casolorz
casolorz

Reputation: 9574

I think I figured out the issue. There was a possibility given certain conditions that the onCreate of my Application class would disable the component before the onCreate on the Activity got a chance to start even when started using that component alias.

Upvotes: 1

Weverton Peron
Weverton Peron

Reputation: 97

I think that application is not finding path of activity alias that you have created. Try to put .MyComponentName in name attribute in activity-alias.

Upvotes: 0

fractalwrench
fractalwrench

Reputation: 4076

Your manifest doesn't define the full package name for the activity. The manifest documentation states that the name attribute should be either the full class name, or preceded with a period (which substitutes the app's package name).

You can fix your crash by changing the manifest declaration to one of the formats below:

android:name=".MyMainActivity"
android:name="com.example.myapp.mypackage.MyMainActivity"

Upvotes: 2

Related Questions