我的应用程序在调试和运行时工作正常,但是当我创建它的Apk,并打开Apk文件时,它没有打开并给我这个错误。
尝试在空对象引用上调用虚方法'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object,java.lang.Object [])'
我的代码是
private class TaskScan extends AsyncTask<Void, Integer, List<AppsListItem>> {
private int mAppCount = 0;
@Override
protected void onPreExecute() {
if (mOnActionListener != null) {
mOnActionListener.onScanStarted(CleanerService.this);
}
}
@Override
protected List<AppsListItem> doInBackground(Void... params) {
mCacheSize = 0;
final List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(
PackageManager.GET_META_DATA);
publishProgress(0, packages.size());
final CountDownLatch countDownLatch = new CountDownLatch(packages.size());
final List<AppsListItem> apps = new ArrayList<>();
try {
for (ApplicationInfo pkg : packages) {
mGetPackageSizeInfoMethod.invoke(getPackageManager(), pkg.packageName,
new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats,
boolean succeeded)
throws RemoteException {
synchronized (apps) {
publishProgress(++mAppCount, packages.size());
mCacheSize += addPackage(apps, pStats, succeeded);
}
synchronized (countDownLatch) {
countDownLatch.countDown();
}
}
}
);
}
countDownLatch.await();
} catch (InvocationTargetException | InterruptedException | IllegalAccessException e) {
e.printStackTrace();
}
return new ArrayList<>(apps);
}
@Override
protected void onProgressUpdate(Integer... values) {
if (mOnActionListener != null) {
mOnActionListener.onScanProgressUpdated(CleanerService.this, values[0], values[1]);
}
}
@Override
protected void onPostExecute(List<AppsListItem> result) {
if (mOnActionListener != null) {
mOnActionListener.onScanCompleted(CleanerService.this, result);
}
mIsScanning = false;
}
private long addPackage(List<AppsListItem> apps, PackageStats pStats, boolean succeeded) {
long cacheSize = pStats.cacheSize;
//if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
cacheSize += pStats.externalCacheSize;
//}
if (!succeeded || cacheSize <= 0) {
return 0;
}
try {
PackageManager packageManager = getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(pStats.packageName,
PackageManager.GET_META_DATA);
apps.add(new AppsListItem(pStats.packageName,
packageManager.getApplicationLabel(info).toString(),
packageManager.getApplicationIcon(pStats.packageName),
cacheSize));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return cacheSize;
}
}
,另一个类是
private class TaskClean extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
if (mOnActionListener != null) {
mOnActionListener.onCleanStarted(CleanerService.this);
}
}
@Override
protected Boolean doInBackground(Void... params) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
StatFs stat = new StatFs(Environment.getDataDirectory().getAbsolutePath());
try {
if (canCleanInternalCache(CleanerService.this)) {
mFreeStorageAndNotifyMethod.invoke(getPackageManager(),
(long) stat.getBlockCount() * (long) stat.getBlockSize(),
new IPackageDataObserver.Stub() {
@Override
public void onRemoveCompleted(String packageName, boolean succeeded)
throws RemoteException {
countDownLatch.countDown();
}
}
);
} else {
countDownLatch.countDown();
}
if (isExternalStorageWritable()) {
final File externalDataDirectory = new File(Environment
.getExternalStorageDirectory().getAbsolutePath() + "/Android/data");
final String externalCachePath = externalDataDirectory.getAbsolutePath() +
"/%s/cache";
if (externalDataDirectory.isDirectory()) {
final File[] files = externalDataDirectory.listFiles();
for (File file : files) {
if (!deleteDirectory(new File(String.format(externalCachePath,
file.getName())), true)) {
Log.e(TAG, "External storage suddenly becomes unavailable");
return false;
}
}
} else {
Log.e(TAG, "External data directory is not a directory!");
}
} else {
Log.d(TAG, "External storage is unavailable");
}
countDownLatch.await();
} catch (InterruptedException | IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
mCacheSize = 0;
}
if (mOnActionListener != null) {
mOnActionListener.onCleanCompleted(CleanerService.this, result);
}
mIsCleaning = false;
}
private boolean deleteDirectory(File file, boolean directoryOnly) {
if (!isExternalStorageWritable()) {
return false;
}
if (file == null || !file.exists() || (directoryOnly && !file.isDirectory())) {
return true;
}
if (file.isDirectory()) {
final File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
if (!deleteDirectory(child, false)) {
return false;
}
}
}
}
file.delete();
return true;
}
private boolean isExternalStorageWritable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
}
//我正在使用这些类的代码
public void cleanCache() {
mIsCleaning = true;
try{
new TaskClean().execute();
}
catch (NullPointerException e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
检查您是否在清单中附加了EXTERNAL_STORAGE(读/写)权限,检查可能出现的可能空案例,并验证TaskScan和TaskScan是否相互依赖