Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 解析意图的活动时出现问题-resolveActivityInfo()_Android_Android Activity_Android Intent_Resolve - Fatal编程技术网

Android 解析意图的活动时出现问题-resolveActivityInfo()

Android 解析意图的活动时出现问题-resolveActivityInfo(),android,android-activity,android-intent,resolve,Android,Android Activity,Android Intent,Resolve,我正在使用resolveActivityInfo确定我的应用程序是否设置为家庭启动器: PackageManager pm = getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_HOME); userHomePackage = intent.resolveActivityInfo(pm,

我正在使用resolveActivityInfo确定我的应用程序是否设置为家庭启动器:

    PackageManager pm = getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_HOME);   

    userHomePackage = intent.resolveActivityInfo(pm, 0).packageName;
    userHomeActivityClass = intent.resolveActivityInfo(pm, 0).name;     
currentHomeLauncherName = intent.resolveActivityInfo(pm, 0).loadLabel(pm).toString();
它在emulator和我手上的三台android设备上运行得非常好

最近我开始从我的用户那里获取错误报告,错误日志显示resolveActivityInfo失败。正如我所看到的,这只发生在运行android 2.1 update 1的少数手机上。由于这个问题,我已经收到了很多关于我的应用程序的正面评论和一些负面评论

有什么建议吗?有什么问题吗

java.lang.NullPointerException
    at android.os.Parcel.readException(Parcel.java:1224)
    at android.os.Parcel.readException(Parcel.java:1206)
    at android.content.pm.IPackageManager$Stub$Proxy.resolveIntent(IPackageManager.java:1418)
    at android.app.ApplicationContext$ApplicationPackageManager.resolveActivity(ApplicationContext.java:2046)
    at android.content.Intent.resolveActivityInfo(Intent.java:3790)
    at com.myapp.myappname.Launcher.setAsHomeApplicationBeforeFroyo(Launcher.java:336)


这里是TeslaCoil软件公司的Kevin。很抱歉耽误了你的回复。这是我用来在我的应用程序中获取Home组件的方法:

public static ComponentName getHomeComponent(PackageManager PM) {
    Intent home_intent = new Intent("android.intent.action.MAIN");
    home_intent.addCategory("android.intent.category.HOME");
    home_intent.addCategory("android.intent.category.DEFAULT");

    ComponentName cn = home_intent.resolveActivity(PM);
    if (cn == null)
        Log.v(TAG, "[Default] package:null");
    else
        Log.v(TAG, "[Default] package:" + cn.getPackageName() + " class:" + cn.getClassName());

    return cn;
}

resolveActivity通常不会返回null,因为如果没有默认设置,它似乎会返回resolver活动。但在某些手机或其他东西上,它可能会返回null,只是为了保持有趣。可能resolveActivityInfo调用了resolveActivity,但没有正确处理空情况。

对android源代码进行了一些研究,我现在倾向于同意Commonware的观点,即我的代码是正确的。实际上,我在3周前重新设计了它,使用packagemanager.resolveActivity而不是intent.resolveActivity:

this.pm = context.getPackageManager();

    try {
        Intent homeintent = new Intent(Intent.ACTION_MAIN);
        homeintent.addCategory(Intent.CATEGORY_HOME);
        homeintent.addCategory(Intent.CATEGORY_DEFAULT);// seems not needed here since This is a synonym for
                                // including
        // the CATEGORY_DEFAULT in your supplied Intent per doc

        this.resolveInfo = pm.resolveActivity(homeintent, PackageManager.MATCH_DEFAULT_ONLY);

        ActivityInfo activityInfo = resolveInfo.activityInfo;

        userHomeLauncherPackage = activityInfo.packageName;
        userHomeLauncherClass = activityInfo.name;

        userHomeLauncherName = activityInfo.loadLabel(pm).toString();

        if (userHomeLauncherClass.contains("ResolverActivity"))
        userHomeLauncherName = "";

    } catch (Exception e) {
        throw new Exception(e);
    }
这没有帮助,所以仍然偶尔会出现这些错误

基于源代码

ComponentName Intent.resolveActivity(PackageManager pm)

ActivityInfo Intent.resolveActivityInfo(PackageManager pm,int标志)

调用android.app.ContextImpl类中定义的相同方法:

ResolveInfo=pm.resolveActivity(仅此,PackageManager.MATCH_默认值_)

它的定义如下:

        @Override
    public ResolveInfo resolveActivity(Intent intent, int flags) {
        try {
            return mPM.resolveIntent(
                intent,
                intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                flags);
        } catch (RemoteException e) {
            throw new RuntimeException("Package manager has died", e);
        }
    }

因此,在这一点上,我得出结论,我对此无能为力:(

您遇到的特殊例外似乎不是源于您的代码。可能是一些人正在使用的有缺陷的改装ROM。感谢您的回复!我现在有更多的报告-过去两天有30多个-所有这些报告都来自不同运营商的三星手机,都运行在android 2.1-update1上。但我也有使用相同手机型号并运行在2.1上的用户的e 5星级率…算了吧…谢谢Kevin!intent.resolveActivity与intent.resolveActivityInfo几乎相同,错误发生在这些方法中,因此我无法在null上检查结果,因为它在此之前失败。我现在使用的是3周的pm.resolveActivity(仅限homeintent、PackageManager.MATCH_DEFAULT_)相反,在2.1上运行的手机每天都会出现一些类似的错误。我想这是固件中的错误,不是我的。我看到了同样的问题,并得出了相同的结论。你向谷歌报告了吗?我不认为我们应该责怪谷歌-这段代码在emulator中运行良好,我尝试了所有版本的api。所以它一定是有问题的制造固件。此问题也会在Nexus7上发生:
PackageManager=context.getPackageManager();List activities=manager.QueryInputActivities(intent,0);
        @Override
    public ResolveInfo resolveActivity(Intent intent, int flags) {
        try {
            return mPM.resolveIntent(
                intent,
                intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                flags);
        } catch (RemoteException e) {
            throw new RuntimeException("Package manager has died", e);
        }
    }