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

最佳答案

来自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,因为如果没有默认设置,它似乎会返回resolver Activity 。但是在某些手机上,它可能会返回null只是为了使事情变得有趣。也许resolveActivityInfo调用resolveActivity,但不能正确处理null情况。

10-08 15:32