kotlin初心者です。検索を搭載したアプリをつくり始めたのですが、検索を実行するとアプリが落ちてしまいます。お判りになる方いらっしゃいますでしょうか?
1.発生している問題
検索機能を実装した「setOnQueryTextListener」を検索バーにセット後、検索を実行するとエラーメッセージを
表示せず、アプリがダウンします。
※下記のソースコードの「検索クエリの実行」まではうまくいっているようで、Logcatで下記のように表示されます。その後、「銘柄検索画面に銘柄リストを表示する」のところまで、実装すると、アプリがおちてしまいます。
I/info: submited keyword is 1 I/info: [StockInfo(id=1, name=極洋, code=1301, industrial_code=null, market_division=東証P, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=2, name=ダイワ上場投信-トピックス, code=1305, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=3, name=NEXT FUNDS TOPIX連動型上場投信, code=1306, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=4, name=上場インデックスファンドTOPIX, code=1308, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=5, name=NEXT FUNDS ChinaAMC・中国株式・上証50連動型上場投信, code=1309, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=6, name=NEXT FUNDS TOPIX Core 30連動型上場投信, code=1311, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=7, name=NEXT FUNDS ラッセル野村小型コア・インデックス連動型上場投信, code=1312, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=8, name=サムスンKODEX200証券上場指数投資信託[株式], code=1313, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=9, name=NEXT FUNDS 日経300株価指数連動型上場投信, code=1319, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000), StockInfo(id=10, name=ダイワ上場投信-日経225, code=1320, industrial_code=null, market_division=ETF・ETN, country=jp, created_at=20220131000000, updated_at=20220131000000)]
2.該当のソースコード
SearchFragment.kt
/** * A simple [Fragment] subclass. * Use the [SearchFragment.newInstance] factory method to * create an instance of this fragment. */ class SearchFragment : Fragment() { // TODO: Rename and change types of parameters private var param1: String? = null private var param2: String? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { param1 = it.getString(ARG_PARAM1) param2 = it.getString(ARG_PARAM2) } } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_search, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // 検索バーに検索イベントリスナーをセット val search_stocks_bar = view.findViewById<SearchView>(R.id.search_stocks_bar) search_stocks_bar.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(keyword: String?): Boolean { Log.i("info", "submited keyword is " + keyword) GlobalScope.launch { if (keyword != null) { // 検索クエリの実行 val stockInfoRep = StockInfoRepository(activity!!.applicationContext) var stockInfos = stockInfoRep.searchByKeyword(keyword!!) Log.i("info", stockInfos.toString()) // 銘柄検索画面に銘柄リストを表示する val lvStocList = activity!!.findViewById<ListView>(R.id.lvStockInfoList) val adapter = StockInfoListAdapter(activity!!.applicationContext, stockInfos) lvStocList.adapter = adapter } } return true } override fun onQueryTextChange(keyword: String?): Boolean { Log.i("info", "inputed keyword is " + keyword) keyword?.let { Log.i("info", keyword) } return true } }) } companion object { /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment SearchFragment. */ // TODO: Rename and change types and number of parameters @JvmStatic fun newInstance(param1: String, param2: String) = SearchFragment().apply { arguments = Bundle().apply { putString(ARG_PARAM1, param1) putString(ARG_PARAM2, param2) } } } }
StockInfoList.kt
package com.example.sampledev.view import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.TextView import com.example.sampledev.R import com.example.sampledev.sqlite.entity.StockInfo class StockInfoListAdapter(context: Context, var stockInfos: List<StockInfo>): ArrayAdapter<StockInfo>(context, 0, stockInfos) { private val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val stockInfo = stockInfos[position] var view =convertView if (convertView == null) { view = layoutInflater.inflate(R.layout.stock_info_list, parent, false) } val name = view?.findViewById<TextView>(R.id.name) name?.text = stockInfo.name return view!! } }
3.環境など
バージョン情報等の参考情報
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' } android { compileSdk 32 defaultConfig { applicationId "com.example.sampledev" minSdk 27 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' // Room(データベース関連のライブラリ) def room_version = "2.4.1" implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" // コルーチンとコルーチンスコープ(非同期処理関連のライブラリ) implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
該当のソースコードのタイトル「StockInfoList.kt」は「StockInfoListAdapter.kt」の誤りでした。
回答1件
あなたの回答
tips
プレビュー