2012-03-21 20 views
0

下載管理器是SDK 2.3,我想在< 2.3設備上使用它,所以我複製從源代碼,但自定義下載管理器,但初始化它無法通過系統的

我把它叫做:

public long startDownload(Uri downloadUri, boolean onlyWifi) 
{ 

    DownloadManager.Request request = new DownloadManager.Request(downloadUri); 
    request.setTitle("Test Download File"); // 
    request.setDescription("App Name"); 
    //request.setDestinationInExternalFilesDir(context, dirType, subPath); 
    request.setDestinationInExternalPublicDir("/sdcard", "test.mp4"); 


    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); 

    final DownloadManager dManager = (DownloadManager)TestDownloadManager.this.getSystemService("download"); 
    return dManager.enqueue(request); 
} 

但它給我:

03-21 06:57:21.181: ERROR/AndroidRuntime(420): Caused by: java.lang.NullPointerException 
03-21 06:57:21.181: ERROR/AndroidRuntime(420):  at com.test.DownloadManager.TestDownloadManager.startDownload(TestDownloadManager.java:32) 
03-21 06:57:21.181: ERROR/AndroidRuntime(420):  at com.test.DownloadManager.TestDownloadManager.onCreate(TestDownloadManager.java:16) 
03-21 06:57:21.181: ERROR/AndroidRuntime(420):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 

我在

發佈download.java

回答

1

因爲你根本無法使用下載管理器上< 2.3 當您在具有API級別< 9設備調用getSystemService,它給你空。這就是爲什麼你得到空指針異常。

相關問題