前提・実現したいこと
APIを利用して取得したデータをRecyclerViewで表示したいのですが、
アイコンの表示がうまくできません。
アイコンを表示させるためにGlideライブラリーを利用して表示させようとしておりましたが、エラーが発生してしまいました。
発生している問題・エラーメッセージ
E/AndroidRuntime: FATAL EXCEPTION: main
Process: jp.co.stv_tech.kotlin232, PID: 28616
java.lang.NullPointerException: Argument must not be null
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:23)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:669)
at jp.co.stv_tech.kotlin232.MainActivity$WeatherInfoReceiver.onPostExecute(MainActivity.kt:155)
at jp.co.stv_tech.kotlin232.MainActivity$WeatherInfoReceiver.onPostExecute(MainActivity.kt:113)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
import android.annotation.SuppressLint import android.graphics.Color import android.os.AsyncTask import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.appcompat.widget.Toolbar import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.google.android.material.appbar.CollapsingToolbarLayout import kotlinx.android.synthetic.main.row.* import org.json.JSONObject import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL class MainActivity : AppCompatActivity() { val weatherList: MutableList<MutableMap<String, String>> = mutableListOf() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Log.i("MainActivity", "onCreate") val toolbar = findViewById<Toolbar>(R.id.toolbar) toolbar.setLogo(R.mipmap.ic_launcher) setSupportActionBar(toolbar) val toolbarLayout = findViewById<CollapsingToolbarLayout>(R.id.toolbarLayout) toolbarLayout.title = getString(R.string.toolbar_title) toolbarLayout.setExpandedTitleColor(Color.WHITE) toolbarLayout.setCollapsedTitleTextColor(Color.LTGRAY) val lvWeatherList = findViewById<RecyclerView>(R.id.lvWeatherList) val layout = LinearLayoutManager(applicationContext) lvWeatherList.layoutManager = layout val weatherList = createWeatherList() val adapter = RecyclerListAdapter(weatherList) lvWeatherList.adapter = adapter val decorator = DividerItemDecoration(applicationContext, layout.orientation) lvWeatherList.addItemDecoration(decorator) //val url = "https://i.picsum.photos/id/777/400/500.jpg" //Glide.with(this).load(url).into(lvImage) } public override fun onStart() { Log.i("MainActivity", "onStart") val receiver = WeatherInfoReceiver() receiver.execute("130010") super.onStart() } private fun createWeatherList():MutableList<MutableMap<String,String>> { Log.i("MainActivity", "createWeatherList") return weatherList } private inner class RecyclerListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){ var tvWeatherDesc: TextView = itemView.findViewById(R.id.tvWeatherDesc) var tvWeatherDate: TextView = itemView.findViewById(R.id.tvWeatherDate) var tvWeather: TextView = itemView.findViewById(R.id.tvWeather) var lvImage: ImageView = itemView.findViewById(R.id.lvImage) } private inner class RecyclerListAdapter(private val _listData: MutableList<MutableMap<String, String>>): RecyclerView.Adapter<RecyclerListViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerListViewHolder { Log.i("MainActivity", "onCreateViewHolder") val inflater = LayoutInflater.from(applicationContext) val view = inflater.inflate(R.layout.row, parent, false) return RecyclerListViewHolder(view) } override fun onBindViewHolder(holder: RecyclerListViewHolder, position: Int) { Log.i("MainActivity", "onBindViewHolder") val item = _listData[position] Log.i("MainActivity", "$position") val description = item["description"] as? String val date = item["date"] as? String val telop = item["telop"] as? String val image = item["image"] as? String holder.tvWeatherDesc.text = description holder.tvWeatherDate.text = date holder.tvWeather.text = telop // holder.lvImage.imageview = image } override fun getItemCount(): Int { Log.i("MainActivity", "getItemCount") return _listData.size } } @SuppressLint("StaticFieldLeak") private inner class WeatherInfoReceiver(): AsyncTask<String, String, String>() { override fun doInBackground(vararg params: String): String { Log.i("MainActivity", "doInBackground") val id = params[0] val urlStr = "http://weather.livedoor.com/forecast/webservice/json/v1?city=${id}" val url = URL(urlStr) val con = url.openConnection() as HttpURLConnection con.requestMethod = "GET" con.connect() val stream = con.inputStream val result = is2String(stream) con.disconnect() stream.close() return result } override fun onPostExecute(result: String) { Log.i("MainActivity", "onPostExecute") val rootJSON = JSONObject(result) val forecasts = rootJSON.getJSONArray("forecasts") val description = rootJSON.getJSONObject("description") Log.i("MainActivity", "description:$description") for (i in 0..1) { val forecastsNow = forecasts.getJSONObject(i) Log.i("MainActivity", "${i}") val telop = forecastsNow.getString("telop") Log.i("MainActivity", "telop:$telop") val date = forecastsNow.getString("date") Log.i("MainActivity", "date:$date") val image = forecastsNow.getJSONObject("image") Log.i("MainActivity", "image:$image") val url = image.getString("url") Log.i("MainActivity", "url:$url") Glide.with(this@MainActivity).load(url).into(lvImage) val weather = mutableMapOf("telop" to telop, "date" to date, "url" to url) weatherList.add(weather) } } } private fun is2String(stream: InputStream): String { Log.i("MainActivity", "is2String") val sb = StringBuilder() val reader = BufferedReader(InputStreamReader(stream, "UTF-8")) var line = reader.readLine() while (line != null) { sb.append(line) line = reader.readLine() } reader.close() return sb.toString() } }
該当のソースコード
kotlin
試したこと
上記にも記載しましたが、Glideライブラリーを試しました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー