解决意图活动的问题 -resolveActivityInfo()

发布于 2024-10-15 02:49:07 字数 3489 浏览 7 评论 0原文

我正在使用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();

它在模拟器和我手头上的三个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)

或者

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myappname/com.myapp.myappname.Launcher}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2497)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2513)
    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1864)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4370)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: 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.showHomeChooserDialog(Launcher.java:141)
    at com.myapp.myappname.Launcher.showNextActivity(Launcher.java:122)
    at com.myapp.myappname.Launcher.onCreate(Launcher.java:59)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
    ... 11 more
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.showHomeChooserDialog(Launcher.java:141)

I am using resolveActivityInfo to determine if my app was set as a home launcher:

    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();

it works great on emulator and three android devices I have on hands.

Recently I started getting error reports from my users and error log shows that resolveActivityInfo is failing. This happens only on a few phones running on android 2.1 update 1 as I can see. I've received already many positive comments on my app and a few negative because of this issue.

Any advice what could be wrong?

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)

OR

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myappname/com.myapp.myappname.Launcher}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2497)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2513)
    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1864)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4370)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: 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.showHomeChooserDialog(Launcher.java:141)
    at com.myapp.myappname.Launcher.showNextActivity(Launcher.java:122)
    at com.myapp.myappname.Launcher.onCreate(Launcher.java:59)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
    ... 11 more
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.showHomeChooserDialog(Launcher.java:141)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光倒影 2024-10-22 02:49:07

来自 TeslaCoil Software 的 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,因为如果没有默认设置,它似乎会返回解析器活动。但在某些手机或其他设备上,它可能会返回 null 只是为了让事情变得有趣。也许resolveActivityInfo 调用了resolveActivity 但没有正确处理null 情况。

Kevin from TeslaCoil Software here. Sorry for the delay in getting back to you. This is what I'm using to get the Home component in my apps:

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 doesn't normally return null, as it seems to return the resolver activity if there is no default set. But on some phones or something it might return null just to keep things interesting. Perhaps resolveActivityInfo calls resolveActivity but doesn't handle the null case properly.

走过海棠暮 2024-10-22 02:49:07

对 android 源码做了一些研究,我现在倾向于同意 Commonsware 的观点,即我的代码是正确的。我实际上在三周前重新设计了它,使用 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 flags)

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

ResolveInfo info =pm.resolveActivity(this, PackageManager.MATCH_DEFAULT_ONLY)

其定义如下:

        @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);
        }
    }

所以此时我得出的结论是我对此无能为力:(

did some research with android source and i tend to agree now with Commonsware that my code is right. I actually redesigned it 3 weeks ago to use packagemanager.resolveActivity instead of 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);
    }

It did not help so still getting these errors occasionally...

Based on the source code,

ComponentName Intent.resolveActivity (PackageManager pm)

or

ActivityInfo Intent.resolveActivityInfo (PackageManager pm, int flags)

call the same method defined in android.app.ContextImpl class:

ResolveInfo info = pm.resolveActivity(this, PackageManager.MATCH_DEFAULT_ONLY)

and it is defined like:

        @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);
        }
    }

so at this point i came to conclusion that I cannot do anything about this :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文