前提・実現したいこと
ボタンを押すと画像をダウンロードするアプリを作りたい
発生している問題・エラーメッセージ
lateinit property urlConnection has not been initialized
該当のソースコード
GetImage
1class GetImage(context: Context, var url: String) : GetImageBase<Bitmap>(context) { 2 private lateinit var urlConnection: HttpURLConnection 3 4 override fun loadInBackground(): Bitmap? { 5 Log.v("nullpo", "loadInBackGround") 6// urlConnection = (url as HttpURLConnection?)!! 7 8 val url: URL = URL(url) 9 10 try { 11 12 urlConnection = (url.openConnection() as HttpURLConnection).also { 13 it.requestMethod = "GET" 14 it.connectTimeout = 1000 15 it.readTimeout = 10000 16 it.connect() 17 } 18 return when (urlConnection.responseCode) { 19 HttpsURLConnection.HTTP_OK -> { 20 BitmapFactory.decodeStream(urlConnection.inputStream) 21 } 22 else -> { 23 null 24 } 25 } 26 } catch (e: Exception) { 27 return null 28 } finally { 29 urlConnection.disconnect() 30 } 31 } 32}
E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1 Process: jp.co.stv_tech.test_download_2, PID: 4116 java.lang.RuntimeException: An error occurred while executing doInBackground() at androidx.loader.content.ModernAsyncTask$3.done(ModernAsyncTask.java:164) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:762) Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.net.HttpURLConnection at jp.co.stv_tech.test_download_2.GetImage.loadInBackground(GetImage.kt:16) at jp.co.stv_tech.test_download_2.GetImage.loadInBackground(GetImage.kt:11) at androidx.loader.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:307) at androidx.loader.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:60) at androidx.loader.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:48) at androidx.loader.content.ModernAsyncTask$2.call(ModernAsyncTask.java:141) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:762)
試したこと
調べてみると初期化されていないことに対してのエラーでした。
さらに初期化=代入との記載もありましたので下記のコードを試してみました。
urlConnection = (url as HttpURLConnection?)!!
しかし、その結果は下記のようなエラーがでてきました。
String cannot be cast to java.net.HttpURLConnection
初期化するために私の試したコードは何が間違っているのでしょうか
どなたかご教示お願いいたします。
E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1 Process: jp.co.stv_tech.test_download_2, PID: 2831 java.lang.RuntimeException: An error occurred while executing doInBackground() at androidx.loader.content.ModernAsyncTask$3.done(ModernAsyncTask.java:164) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) Caused by: kotlin.UninitializedPropertyAccessException: lateinit property urlConnection has not been initialized at jp.co.stv_tech.test_download_2.GetImage.loadInBackground(GetImage.kt:38) at jp.co.stv_tech.test_download_2.GetImage.loadInBackground(GetImage.kt:11) at androidx.loader.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:307) at androidx.loader.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:60) at androidx.loader.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:48) at androidx.loader.content.ModernAsyncTask$2.call(ModernAsyncTask.java:141) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764)
回答1件
あなたの回答
tips
プレビュー