Search code examples
androidandroid-activityandroid-fragmentactivityandroid-music-player

ActivityNotFoundException on some Android devices


I have an Android-App uploaded in the App-Store (SDK-Versions 15-25). Crashlytics is reporting me the following Exception:

Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 sel=act=android.intent.action.MAIN cat=[android.intent.category.APP_MUSIC]} }
       at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1776)
       at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
       at android.app.Activity.startActivityForResult(Activity.java:3798)
       at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
       at android.app.Activity.startActivityForResult(Activity.java:3749)
       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
       at android.app.Activity.startActivity(Activity.java:4079)
       at android.app.Activity.startActivity(Activity.java:4047)
       at com.myapp.myappname.ui.activity.MainActivity.onOptionsItemSelected(MainActivity.java:467)
       at android.app.Activity.onMenuItemSelected(Activity.java:2934)
       at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
       at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
       at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
       at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
       at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
       at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:206)
       at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:776)
       at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
       at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
       at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
       at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:959)
       at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:623)
       at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:154)
       at android.view.View.performClick(View.java:4807)
       at android.view.View$PerformClick.run(View.java:20106)
       at android.os.Handler.handleCallback(Handler.java:815)
       at android.os.Handler.dispatchMessage(Handler.java:104)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5576)
       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:955)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

This is the method in MainActivity:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.player) {
            Timber.i( "Added onClick listener to ImageView ivPlayer.");
            Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;
           } else if (id == R.id.logout) {
            Toast.makeText(getApplicationContext(), "Logging out...", Toast.LENGTH_LONG).show();
            logout();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Previously I used the following code with the same exception:

 Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

Operating Systems with this exception are 4.4.4 and 5.0.1.

Any ideas for this exception and proposals to avoid it?


Solution

  • Operating Systems with this exception are 4.4.4 and 5.0.1

    No. You happen to have received crash notices for those OS versions. There are ~2 billion Android devices, and this sort of problem can happen on any of them.

    Any ideas for this exception

    You are assuming that each and every one of those ~2 billion Android devices have one or more apps with an activity matching Intent.ACTION_MAIN and Intent.CATEGORY_APP_MUSIC. There is no requirement for any of those devices to have such an activity.

    proposals to avoid it?

    Option #1: Wrap your startActivity() call in a try/catch block and catch the ActivityNotFoundException, then tell the user that you cannot find a suitable app.

    Option #2: Use PackageManager and queryIntentActivities() to see if there are any matches for the Intent. If there are none, do not call startActivity(), then tell the user that you cannot find a suitable app.