java.lang.NullPointerException:尝试在启动通知时调用接口方法'java.util.Iterator java.lang.Iterable.iterator()' [英] java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' when starting a notification

查看:340
本文介绍了java.lang.NullPointerException:尝试在启动通知时调用接口方法'java.util.Iterator java.lang.Iterable.iterator()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我的一些用户正在收到此异常.我不知道如何复制它,我只有关于Crashlytics的报告.似乎深入到Google的代码中.在使用此代码的成千上万中,只有39个例外.

I noticed some of my users are getting this exception. I don't know how to reproduce it, I only have the reports on Crashlytics. Seems to be deep inside Google's code. Out of thousands who have used this code only 39 have had the exception.

任何主意可能有什么问题吗?

Any idea what might be wrong?

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference
       at android.app.ApplicationPackageManager.getUserIfProfile(ApplicationPackageManager.java:2141)
       at android.app.ApplicationPackageManager.getUserBadgeForDensity(ApplicationPackageManager.java:997)
       at android.app.Notification$Builder.getProfileBadgeDrawable(Notification.java:2877)
       at android.app.Notification$Builder.hasThreeLines(Notification.java:3092)
       at android.app.Notification$Builder.build(Notification.java:3646)
       at android.support.v4.app.NotificationCompatApi21$Builder.build(NotificationCompatApi21.java:136)
       at android.support.v7.app.NotificationCompat$LollipopExtender.build(NotificationCompat.java:504)
       at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:835)
       at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1752)
       at mycode.startNotification(mycode.java:361)

谢谢.

推荐答案

ApplicationPackageManager.java
private UserInfo getUserIfProfile(int userHandle) {
    List<UserInfo> userProfiles = getUserManager().getProfiles(UserHandle.myUserId());
    for (UserInfo user : userProfiles) {
        if (user.id == userHandle) {
            return user;
        }
    }
    return null;
}

public List<UserInfo> getProfiles(int userHandle) {
    try {
        return mService.getProfiles(userHandle, false /* enabledOnly */);
    } catch (RemoteException re) {
        Log.w(TAG, "Could not get user list", re);
        return null;
    }
}

因此,如果AIDL请求出现问题,或禁用了用户的个人资料,则ApplicationPackageManager.java代码中将包含NPE.我认为无法避免这种情况,建议您将通知创建内容包装在try {} catch块

So, if there is something wrong with AIDL request, or user's profile is disabled you will have NPE in ApplicationPackageManager.java code. I think it is impossible to prevent this situation, and I advise you to wrap notification creation in try{}catch block

这篇关于java.lang.NullPointerException:尝试在启动通知时调用接口方法'java.util.Iterator java.lang.Iterable.iterator()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆