Reputation: 531
I am facing a head scratching issue and not able to resolve. Can someone here help...
I have one activity (parent) that starts another activity(child). And I am initiating Download Manager from child activity. When I am clicking a button on child activity to view the DOWNLOADS, it opens up the DOWNLOAD manager and shows the downloads. Till this point everything goes fine. But when I return from this DOWNLOAD View Window by pressing return key, most of the time it is crashing (specially when download is completed). Don't know why..
Note: When I initiate Download Manager from parent activity, all works well. But I need to do it from child. Any help will be appreciated.
I am starting child activity like:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> p, View v, int position, long id) {
Intent i = new Intent(AudioDownloadActivity.this,CDPageActivity.class);
i.putExtra("audioCDURL", "http://bkdrluhar.com/00-htm/" + hiddenText);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AudioDownloadActivity.this.startActivity(i);
}});
In Child activity, defining and initiating Broadcast Receiver for Download Manager like:
private long enqueue;
private DownloadManager dm;
/*some code */
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
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)) {
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
/*some code */
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(downloadURL));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(true).setTitle("BK Asset")
.setDescription(tempFile)
.setDestinationUri(Uri.fromFile(new File(MainActivity.downloadFileLocation,tempFile)));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
enqueue = dm.enqueue(request);
And the button press which pops shows up the DOWNLOADS:
public void onAudioCDDownloadClick(View v){
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
Log cat for this crash is:
FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.os.bkdownloader (has extras) } in com.os.bkdownloader.AudioDownloadActivity$1@416b8ad8
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:995)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.os.bkdownloader.AudioDownloadActivity$1.onReceive(AudioDownloadActivity.java:158)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
Upvotes: 0
Views: 1042