Getting NullPointerException for querySummary method while calculating data usage

时间:2017-08-04 12:37:09

标签: android android-6.0-marshmallow usagestatsmanager networkstatsmanager

I'm using querySummary() method of NetworkStatsManager to calculate the data usage,it gives me proper usage but sometimes it throws NullPointerException for this method.

Error in UsageTestService:

java.lang.NullPointerException: 
at android.os.Parcel.readException (Parcel.java:1626)
at android.os.Parcel.readException (Parcel.java:1573)
at android.net.INetworkStatsSession$Stub$Proxy.getSummaryForAllUid 
(INetworkStatsSession.java:355)
at android.app.usage.NetworkStats.startSummaryEnumeration 
(NetworkStats.java:330)
at android.app.usage.NetworkStatsManager.querySummary 
(NetworkStatsManager.java:166)
at com.test.services.UsageTestService$1.run (UsageTestService.java:172)

My code :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            NetworkStats networkStats = null;
                            try {

                                networkStatsManager = (NetworkStatsManager) getApplicationContext().getSystemService(Context.NETWORK_STATS_SERVICE);
                                networkStats = networkStatsManager.querySummary(0, getSubscriberId(UsageTestService.this, ConnectivityManager.TYPE_MOBILE), 0, System.currentTimeMillis());

                            } catch (RemoteException e) {

                                e.printStackTrace();
                            }

                            if(networkStats!=null)
                            {

                                while (networkStats.hasNextBucket()) {

                                    NetworkStats.Bucket bucket = new NetworkStats.Bucket();
                                    networkStats.getNextBucket(bucket);

                                    int uid = bucket.getUid();
                                    double rx1 = bucket.getRxBytes();
                                    double tx1 = bucket.getTxBytes();
                                    int state1 = bucket.getState();
                                    double totalRxTx1 = rx1+ tx1;
                                    }
                                    }
                                    }

What will be the issue?

1 个答案:

答案 0 :(得分:0)

我们偶然发现同样的问题,似乎是一个平台错误。目前,仍然在主分支中不固定。

这是当前平台代码的摘录:

        @Override
        public NetworkStats getSummaryForAllUid(
                NetworkTemplate template, long start, long end, boolean includeTags) {
            try {
                @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
                final NetworkStats stats =
                        getUidComplete().getSummary(template, start, end, accessLevel);
                if (includeTags) {
                    final NetworkStats tagStats = getUidTagComplete()
                            .getSummary(template, start, end, accessLevel);
                    stats.combineAllValues(tagStats);
                }
                return stats;
            } catch (NullPointerException e) {
                // TODO: Track down and fix the cause of this crash and remove this catch block.
                Slog.wtf(TAG, "NullPointerException in getSummaryForAllUid", e);
                throw e;
            }
        }

https://github.com/android/platform_frameworks_base/blob/master/services/core/java/com/android/server/net/NetworkStatsService.java#L572