I am attempting to implement DownloadManager in my current Android project to download an mp3 from a URL. The Android SDK version that I'm using is 2.3.3. The download is started by a button click even. From there a string is passed to the downloadPodcast method to begin the download. The problem I've ran into is I am getting a null pointer exception when attempting to start the download. I have ran through debugger and verified that the URL string is properly passed to this method and contains a valid string value. The specific line that is failing is:
downloadId= dm.enqueue(new DownloadManager.Request(Uri.parse(podcastUrl))
After debugging I believe I have narrowed it down to 'dm'. The value of this item is always "null" even after, what I believe is, properly initializing it in the onCreate method. I've looked through several examples and believe I am implementing this properly, I'm just at a dead end and not sure why this is still null.
Main Activity code:
public class Podcasts extends ListActivity{
private DownloadManager dm;
private long downloadId=-1L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.podcasts);
dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Download method:
public void downloadPodcast (String podcastUrl){
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir();
downloadId= dm.enqueue(new DownloadManager.Request(Uri.parse(podcastUrl))
.setAllowedOverRoaming(false)
.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.mp3")
.setTitle("TEST"));
}
Added Logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at tv.undignified.android.Podcasts.downloadPodcast(Podcasts.java:275)
at tv.undignified.android.Podcasts$CustomAdapter$2.onClick(Podcasts.java:218)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)