我正在使用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更新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)
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)
答案 0 :(得分:4)
凯特来自TeslaCoil Software。很抱歉延迟回复你。这就是我用来在我的应用程序中获取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 case。
答案 1 :(得分:1)
用android源做了一些研究,我现在倾向于同意Commonsware我的代码是对的。我实际上在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 flags)
调用android.app.ContextImpl类中定义的相同方法:
ResolveInfo信息= pm.resolveActivity(此,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);
}
}
所以在这一点上我得出的结论是我对此无能为力:(