This AsyncTask class should be static or leaks might occurリークのないAsyncTask
なぜ私のAsyncTaskは機能しませんか?
コール:PlacesTask(this).execute(...)
コード:
private class PlacesTask internal constructor(activity: MainActivity) : AsyncTask<String, Int, String>() {
var data: String? = null
private val mRef: WeakReference<MainActivity> = WeakReference(activity)
override fun doInBackground(vararg url: String): String? {
try {
data = MainActivity().downloadUrl(url[0])
} catch (e: Exception) {
Log.d("Background Task", e.toString())
}
return data
}
override fun onPostExecute(result: String) {
val asyncTaskLeak = mRef.get()
if (asyncTaskLeak != null) SetPlaceTask().execute(result)
}
}
private class SetPlaceTask : AsyncTask<String, Int, List<HashMap<String, String>>>() {
var places: List<HashMap<String, String>>? = null
override fun doInBackground(vararg jsonData: String): List<HashMap<String, String>>? {
try {
places = PlaceJSONParser().parse(JSONObject(jsonData[0]))
} catch (e: Exception) {
Log.d("Exception", e.toString())
}
return places
}
override fun onPostExecute(list: List<HashMap<String, String>>) {
...
}
}
@Throws(IOException::class)
private fun downloadUrl(strUrl: String): String {
var data = ""
var iStream: InputStream? = null
var urlConnection: HttpURLConnection? = null
try {
urlConnection = URL(strUrl).openConnection() as HttpURLConnection
urlConnection.connect()
iStream = urlConnection.inputStream
val br = BufferedReader(InputStreamReader(iStream!!))
val sb = StringBuilder()
var line: String? = null
while ({line = br.readLine(); line }() != null) sb.append(line)
data = sb.toString()
br.close()
} catch (e: Exception) {
Log.d("downloading url", e.toString())
} finally {
if (iStream != null) iStream.close()
if (urlConnection != null) urlConnection.disconnect()
}
return data
}
Error: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter result
助けてください。私は何が欠けていますか?