在计算数据使用情况时获取 NullPointerException for querySummary 方法



我正在使用NetworkStatsManager的querySummary((方法来计算数据使用量,它给了我正确的用法,但有时它会为此方法抛出NullPointerException。

使用测试服务中的错误:

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)

我的代码 :

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

会有什么问题?

我们偶然发现了同样的问题,这似乎是一个平台错误。目前,在主分支中仍未修复。

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

@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

相关内容

  • 没有找到相关文章

最新更新