在下载apk包时,下面代码中this.mExecutor.execute(this.mConnectTask);始终报空指针
异常信息:
java.lang.NullPointerException: Attempt to invoke interface method 'void java.util.concurrent.Executor.execute(java.lang.Runnable)' on a null object reference
at com.aspsine.multithreaddownload.core.DownloaderImpl.connect(DownloaderImpl.java:213)
at com.aspsine.multithreaddownload.core.DownloaderImpl.start(DownloaderImpl.java:76)
at com.aspsine.multithreaddownload.DownloadManager.download(DownloadManager.java:98)
at com.common.common.apkupdate.UpdateApk.downloads(UpdateApk.java:359)
at com.common.common.apkupdate.UpdateApk.access$700(UpdateApk.java:55)
at com.common.common.apkupdate.UpdateApk$3.onDialogOk(UpdateApk.java:266)
at com.common.common.dialog.DialogAlert.onBtnOk(DialogAlert.java:71)
at com.common.common.dialog.DialogAlert.onClick(DialogAlert.java:62)
at android.view.View.performClick(View.java:6392)
at android.view.View$PerformClick.run(View.java:25133)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:7069)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
DownloaderImpl.class中
private void connect() {
this.mConnectTask = new ConnectTaskImpl(this.mRequest.getUri(), this);
this.mExecutor.execute(this.mConnectTask);
}
在jar包中搜寻半天,找到初始化方法为
DownloadManager.class中
public void init(Context context) {
this.init(context, new DownloadConfiguration());
}
public void init(Context context, @NonNull DownloadConfiguration config) {
if (config.getThreadNum() > config.getMaxThreadNum()) {
throw new IllegalArgumentException("thread num must < max thread num");
} else {
this.mConfig = config;
this.mDBManager = DataBaseManager.getInstance(context);
this.mExecutorService = Executors.newFixedThreadPool(this.mConfig.getMaxThreadNum());
this.mDelivery = new DownloadStatusDeliveryImpl(this.mHandler);
}
}
对比其他成功项目没发现有调用init的地方,纳闷为什么相同代码有的可以初始化,有的不能初始化,最后发现在成功的项目中有如下代码
private void initDownloader() {
DownloadConfiguration configuration = new DownloadConfiguration();
configuration.setMaxThreadNum(10);
configuration.setThreadNum(3);
DownloadManager.getInstance().init(getApplicationContext(), configuration);
}
结果一万只草泥马呼啸而过,还真特么的忘了初始化,请原谅我的爆粗,毕竟憋了近四个小时。