- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有带有 searchItem 和 tabHost 的 Activity 。 TabHost 有其他 Activity 。我在数据库中有一个搜索功能,它显示另一个 Activity 中的结果,该 Activity 位于 tabhost 中。但是当您尝试搜索时,它会出错:
kotlin.UninitializedPropertyAccessException: lateinit property llMain has not been initialized.
我理解错误是当一个函数被调用时,它进入 viewTab 并试图调用 llMain。但我不知道如何解决这个问题。
class QuestionsActivity : AppCompatActivity(),WatchActivity.search{
override fun searchQA(newText: String?) {
if (newText != null) {
val arraylis = db.search(newText)
val search_date = Array(date.size) { "" }
val search_id = IntArray(arraylis.size)
val search_kategory = Array(arraylis.size) { "" }
val search_summa = FloatArray(arraylis.size)
ParseList().parse_search(arraylis,search_id,search_date,search_kategory,search_summa)
Log.d("String", newText)
ViewTab(search_id, search_date, search_kategory, search_summa)
}
}
private lateinit var llMain: RecyclerView
val adapter = MyRecyclerViewAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.questions_layout)
llMain = findViewById(R.id.llMain)
//Other code
}
private fun ViewTab(id: IntArray, date: Array<String>, kategory: Array<String>,
summa: FloatArray) {
val numberOfColumns = 1
llMain.layoutManager = GridLayoutManager(this, numberOfColumns)
adapter.MyRecyclerViewAdapter_(this, id, date, kategory, summa)
llMain.adapter = adapter
}
}
class WatchActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onQueryTextChange(newText: String?): Boolean {
(qa as(search)).searchQA(newText)
return false
}
interface search{
fun searchQA(newText: String?)
}
private val qa = QuestionsActivity()
private lateinit var tabHost: TabHost
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.watch_layout)
tabHost = findViewById(R.id.tabhost)
val mLocalActivityManager = LocalActivityManager(this, false)
mLocalActivityManager.dispatchCreate(savedInstanceState) // state will be bundle your activity state which you get in onCreate
tabHost.setup(mLocalActivityManager)
addTab("Расходы", "Расходы", qa::class.java)
tabHost.currentTab = 0
}
private fun addTab(spec: String, indic: String, classname: Class<*>) {
val tabSpec = tabHost.newTabSpec(spec)
tabSpec.setIndicator(indic)
val intent = Intent(this, classname)
tabSpec.setContent(intent)
tabHost.addTab(tabSpec)
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/numb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView19"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"
android:textSize="20sp" android:textColor="@android:color/background_dark" android:layout_marginTop="40dp"
android:layout_marginLeft="4dp" android:layout_marginStart="4dp"/>
<TextView
android:text="@string/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView20"
android:textSize="20sp" app:layout_constraintStart_toEndOf="@+id/textView19"
app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:clickable="true" android:textColor="@android:color/background_dark"
android:layout_marginTop="40dp" android:focusable="true"/>
<TextView
android:text="@string/kate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView21"
android:textSize="20sp" app:layout_constraintStart_toEndOf="@+id/textView19"
app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="168dp"
android:layout_marginStart="168dp" android:textColor="@android:color/background_dark"
android:layout_marginTop="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView22"
android:textSize="20sp" app:layout_constraintStart_toEndOf="@+id/textView19"
app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="320dp"
android:layout_marginStart="320dp" android:textColor="@android:color/background_dark"
android:layout_marginTop="40dp" android:text="@string/sum"/>
<android.support.v7.widget.RecyclerView
android:layout_width="407dp"
android:id="@+id/llMain"
android:layout_height="447dp"
android:layout_x="-3dp"
android:layout_y="-1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="203dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/textView20"/>
<View
android:id="@+id/view"
android:visibility="gone"
android:background="#CC000000"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/multiplie_actions"
android:layout_margin="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
app:fab_addButtonColorNormal="@color/colorPrimaryDark"
app:fab_addButtonColorPressed="@color/colorAccent"
app:fab_addButtonPlusIconColor="@android:color/background_light"
app:fab_labelStyle="@style/menu_lables_style"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="92dp" android:layout_marginRight="92dp"
android:layout_marginTop="10dp" app:layout_constraintTop_toBottomOf="@+id/searchView">
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/groupby"
app:fab_title="Сгруппировать"
app:fab_icon="@drawable/group"
app:fab_colorNormal="#FFFFFF"
app:fab_colorPressed="#BDBDBD"
app:fab_size="normal"
app:layout_constraintBottom_toTopOf="@+id/multiplie_actions"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/drop_table"
app:fab_title="Очистить данные"
app:fab_icon="@drawable/clear"
app:fab_colorNormal="#FFFFFF"
app:fab_colorPressed="#BDBDBD"
app:fab_size="normal"
app:layout_constraintBottom_toTopOf="@+id/multiplie_actions"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
<SearchView
android:layout_width="402dp"
android:layout_height="33dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="4dp" android:layout_marginStart="4dp" app:layout_constraintTop_toTopOf="parent"
android:id="@+id/searchView">
</SearchView>
</android.support.constraint.ConstraintLayout>
最佳答案
首先,您的代码在很多方面都存在严重错误,并且违反了常见的 Android 开发指南,包括命名、模式和良好做法。我建议您熟悉一些 Android 代码(例如由 Google 或 Square 编写的代码)或阅读一些有关 Android 开发的文献。
然而,由于您在使用此行创建的 Activity
实例上调用 (qa as(search)).searchQA(newText)
导致发生错误: private val qa = QuestionsActivity()
。在那里,您覆盖了原始 QuestionsActivity.class
的 onCreate()
方法。 因此,您不再初始化 llMain
并且您收到错误...
简单地说,你没有这一行:
llMain = findViewById(R.id.llMain)
在 onCreate()
方法中,在 Activity
的重写方法中,您在此行中手动构建:private val qa = QuestionsActivity()
:
private val qa = QuestionsActivity()
private lateinit var tabHost: TabHost
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.watch_layout)
tabHost = findViewById(R.id.tabhost)
val mLocalActivityManager = LocalActivityManager(this, false)
mLocalActivityManager.dispatchCreate(savedInstanceState) // state will be bundle your activity state which you get in onCreate
tabHost.setup(mLocalActivityManager)
addTab("Расходы", "Расходы", qa::class.java)
tabHost.currentTab = 0
}
onCreate()
方法中没有llMain
的初始化
关于android - lateinit 属性 llMain(RecyclerView) 尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57328233/
我有带有 searchItem 和 tabHost 的 Activity 。 TabHost 有其他 Activity 。我在数据库中有一个搜索功能,它显示另一个 Activity 中的结果,该 Ac
我是一名优秀的程序员,十分优秀!