質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

668閲覧

画面がフリーズしてしまいます

t_makino

総合スコア22

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2022/04/24 02:30

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' }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

t_makino

2022/04/24 02:39

該当のソースコードのタイトル「StockInfoList.kt」は「StockInfoListAdapter.kt」の誤りでした。
guest

回答1

0

自己解決

フリーズしたのは、自分の環境の問題だったようです(開発PC上で複数のアプリを起動していたため、メモリがいっぱいになっていた?)。すべてのアプリを落として確認したところ、下記のようなエラーメッセージが表示されていました。

Only the original thread that created a view hierarchy can touch its views

上記エラーは、ビューの変更をUIスレッドで行っていないことを原因としている模様。
そこで、コードを下記のように変更したところ、正常に検索結果が表示されるようになりました。
お手数、ご迷惑をおかけしした。

変更前コード

val lvStocList = findViewById<ListView>(R.id.lvStockInfoList) val adapter = StockInfoListAdapter(this@SearchStocksActivity, stockInfos) lvStocList.adapter = adapter

変更後コード

val mainHandler: Handler = Handler(Looper.getMainLooper()) this@SearchStocksActivity.runOnUiThread( Runnable { val lvStocList = findViewById<ListView>(R.id.lvStockInfoList) val adapter = StockInfoListAdapter(this@SearchStocksActivity, stockInfos) lvStocList.adapter = adapter }) }

投稿2022/04/24 11:13

t_makino

総合スコア22

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問