检查是否有多个下载完成?

时间:2014-11-03 14:28:41

标签: java android broadcastreceiver android-download-manager

我的应用首次启动时需要下载10张图片。因此,一旦所有图像都被下载,它应该继续下一个活动。但是当它完成下载第一个图像时它会崩溃。我不知道为什么。在下面附上了Receiver和Logcat的代码。谢谢你。< / p>

BroadcastReceiver receiver = new BroadcastReceiver() {

        @SuppressLint("NewApi")
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                Query query = new Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c
                            .getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c
                            .getInt(columnIndex)) {
                        testtv.setText("Download Complete");

                    }
                }
            }
        }

    };
    registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));

logcat的

11-03 19:49:01.660: E/AndroidRuntime(11857): FATAL EXCEPTION: main
11-03 19:49:01.660: E/AndroidRuntime(11857): Process: com.id.imagedownloader, PID: 11857
11-03 19:49:01.660: E/AndroidRuntime(11857): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.id.imagedownloader (has extras) } in com.id.imagedownloader.MainActivity$1@41bc2aa8
11-03 19:49:01.660: E/AndroidRuntime(11857):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:776)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at android.os.Handler.handleCallback(Handler.java:733)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at android.os.Looper.loop(Looper.java:136)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at android.app.ActivityThread.main(ActivityThread.java:5146)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at java.lang.reflect.Method.invokeNative(Native Method)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at java.lang.reflect.Method.invoke(Method.java:515)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at dalvik.system.NativeStart.main(Native Method)
11-03 19:49:01.660: E/AndroidRuntime(11857): Caused by: java.lang.NullPointerException
11-03 19:49:01.660: E/AndroidRuntime(11857):    at com.id.imagedownloader.MainActivity$1.onReceive(MainActivity.java:50)
11-03 19:49:01.660: E/AndroidRuntime(11857):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
11-03 19:49:01.660: E/AndroidRuntime(11857):    ... 9 more

0 个答案:

没有答案