2014-07-17 2 views
0

'Azure'에 'assetcontainer'라는 컨테이너를 만들었습니다.Android Azure SDK - BLOB 저장소에서 무언가를로드하려고하면 NullPointerException이 발생합니다.

CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString); 
     mClient = account.createCloudBlobClient(); 
     mAssetContainer = mClient.getContainerReference("assetcontainer"); 


     // Download the blob 
     // For each item in the container 
     for (ListBlobItem blobItem : mAssetContainer.listBlobs()) { 
      // If the item is a blob, not a virtual directory 
      if (blobItem instanceof CloudBlockBlob) { 
       // Download the text 
       CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem; 

       try { 
        retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName())); 
       } 
       catch(Exception ex){} 
      } 
     } 

그리고 나는 listBlobs()을 반복하려고 할 때 다음과 같은 예외를 수신하고 있습니다 :

Process: com.training.app.debug, PID: 3284 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.training.app.debug/com.training.app.activities.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350) 
      at android.app.ActivityThread.access$800(ActivityThread.java:163) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:157) 
      at android.app.ActivityThread.main(ActivityThread.java:5335) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:783) 
      at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 
      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 
      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503) 
      at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:145) 
      at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:252) 
      at com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:109) 
      at com.training.azure.AzureStorage.syncAssets(AzureStorage.java:57) 
      at com.training.app.activities.MainActivity.onCreate(MainActivity.java:52) 
      at android.app.Activity.performCreate(Activity.java:5389) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350) 
            at android.app.ActivityThread.access$800(ActivityThread.java:163) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:157) 
            at android.app.ActivityThread.main(ActivityThread.java:5335) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
            at dalvik.system.NativeStart.main(Native Method) 

난을

다음 코드를 만들 수있는 샘플을 따랐습니다 무엇이 잘못 될 수 있는지 확실하지 않습니다 ...이 푸른 하늘과의 버그입니까?

답변

2

this page에서 해결책을 찾았습니다. 저에게 효과적이었습니다.

안드로이드는 메인 쓰레드에서 네트워크 작업을 수행 할 수 없습니다, 그래서 당신은 다음과 같이 비동기 작업에서 코드를 실행해야합니다

private class MyTask extends AsyncTask<Void, Void, Void>{ 
@Override 
protected Void doInBackground(Void... params) { 
    CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString); 
    mClient = account.createCloudBlobClient(); 
    mAssetContainer = mClient.getContainerReference("assetcontainer"); 


    // Download the blob 
    // For each item in the container 
    for (ListBlobItem blobItem : mAssetContainer.listBlobs()) { 
     // If the item is a blob, not a virtual directory 
     if (blobItem instanceof CloudBlockBlob) { 
      // Download the text 
      CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem; 

      try { 
       retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName())); 
      } 
      catch(Exception ex){} 
     } 
    } 
} 
} 

그런 다음 메인 스레드에서 new MyTask().execute()를 호출합니다.

+0

나는 이것도 발견했다! 설명서에이 부분이 언급되어 있지 않으며 실제 오류는 찾기가 약간 어렵습니다. –

+0

@Mr_E이 문제를 해결하기 위해 새 버전의 라이브러리 (0.3.1)를 출시했습니다. [여기] 코드 (https://github.com/Azure/azure-storage-android)를 가져올 수 있습니다. 이제 올바른 오류 메시지가 나타납니다. –

+0

doInBackground()의 작업에서 실행 중이며 여전히이 오류가 발생합니다. 나를 위해 파렴치. – BJV

관련 문제