简体   繁体   English

在某些Android设备上的ActivityNotFoundException

[英]ActivityNotFoundException on some Android devices

I have an Android-App uploaded in the App-Store (SDK-Versions 15-25). 我在应用商店中上传了一个Android应用(SDK版本15-25)。 Crashlytics is reporting me the following Exception: Crashlytics向我报告以下异常:

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 : 这是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. 例外的操作系统是4.4.4和5.0.1。

Any ideas for this exception and proposals to avoid it? 对这种例外情况有什么想法以及避免这种情况的建议?

Operating Systems with this exception are 4.4.4 and 5.0.1 例外的操作系统是4.4.4和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. 大约有20亿个Android设备,并且其中任何一种都可能发生这种问题。

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 . 您假设这20亿个Android设备中的每一个都有一个或多个应用程序,它们的活动与Intent.ACTION_MAINIntent.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. 选项#1:将startActivity()调用包装在try / catch块中,并捕获ActivityNotFoundException ,然后告诉用户您找不到合适的应用程序。

Option #2: Use PackageManager and queryIntentActivities() to see if there are any matches for the Intent . 选项#2:使用PackageManagerqueryIntentActivities()来查看Intent是否匹配。 If there are none, do not call startActivity() , then tell the user that you cannot find a suitable app. 如果没有,则不要调用startActivity() ,然后告诉用户您找不到合适的应用程序。

To your code 给你的代码

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

add the following 添加以下内容

try {
    List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
    if (activities.size() > 0)
        startActivity(intent);
    else
        Toast.makeText(MainActivity.this, "A music player is absent on your device!", Toast.LENGTH_SHORT).show();
} catch (ActivityNotFoundException e) {
    e.printStackTrace();
} catch (SecurityException e){
    e.printStackTrace();
}

致命异常可能是您传递了空对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM