gpt4 book ai didi

java - NPE同时计算apk大小

转载 作者:行者123 更新时间:2023-12-01 19:09:18 24 4
gpt4 key购买 nike

我想计算用户在手机上安装的应用程序的apk文件大小

01-11 15:20:30.373: E/AndroidRuntime(17940):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.lang.Thread.run(Thread.java:1096)
01-11 15:20:30.373: E/AndroidRuntime(17940): Caused by: java.lang.NullPointerException
01-11 15:20:30.373: E/AndroidRuntime(17940): at com.taskmanager.sb.PackageTabActivity$AppDetails.doInBackground(PackageTabActivity.java:119)
01-11 15:20:30.373: E/AndroidRuntime(17940): at com.taskmanager.sb.PackageTabActivity$AppDetails.doInBackground(PackageTabActivity.java:1)
01-11 15:20:30.373: E/AndroidRuntime(17940): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-11 15:20:30.373: E/AndroidRuntime(17940): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-11 15:20:30.373: E/AndroidRuntime(17940): ... 4 more

代码是

       for(int i=0;i<packs.size();i++ ) {

final PackageInfo p = packs.get(i);

if(!isSystemPackage(p))
{
String package_name = p.applicationInfo.packageName;

appname[i] =p.applicationInfo.loadLabel(getPackageManager()).toString();
try
{
getPackageSizeInfo.invoke(pm, package_name, new IPackageStatsObserver.Stub()
{
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException
{

size[i] = pStats.codeSize/1024; // here error come says make i final


}});


}catch(IllegalAccessException a) {}
catch(IllegalArgumentException b) {}
catch(InvocationTargetException c) {}
}


// Log.v("apksize", "appsize = "+sSize+"appname = "+sName);


}

如果我将 i 设置为全局,那么我会得到 NPE,如果我将 i 设置为最终,那么 for 循环会说删除最终

第119行就是appname[i]……我该如何解决这个问题?

感谢您的帮助!!

最佳答案

如果第 119 行是抛出 NullPointerException 的行,并且第 119 行是这一行:

appname[i] =p.applicationInfo.loadLabel(getPackageManager()).toString();

那么您需要处理 p.applicationInfo.loadLabel(getPackageManager()) 返回 null 的情况。

关于java - NPE同时计算apk大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8817581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com