gpt4 book ai didi

Android Azure SDK - 当我尝试从 Blob 存储加载内容时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 21:05:13 25 4
gpt4 key购买 nike

我在 Azure 上创建了一个名为“assetcontainer”的容器。

我按照示例创建了以下代码:

 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)

我不确定这里出了什么问题...这是 azure sdk 的错误吗?

最佳答案

我从 this page 找到了解决方案,这对我有用。

Android不允许在主线程中执行网络操作,因此需要在异步任务中执行代码,如下所示:

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()

关于Android Azure SDK - 当我尝试从 Blob 存储加载内容时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24813230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com