【发布时间】:2019-01-20 22:51:44
【问题描述】:
我找到了一个解决方案并将其作为答案附上
我目前在使用来自我的适配器的信息的对话框中填充布局时遇到问题。数据是从 API 获取并传递到我的数据类中,但由于我试图引用的 recyclerview 在对话框的布局文件中,而不是在我用来调用所述对话框的文件中,因此视图只返回一个 null .
这是我的上下文代码。
CheckboxActivity.kt(只是回调)people_list 返回 null
private val callbackGETUSERS = object : Callback<List<Users>> {
override fun onFailure(call: Call<List<Users>>, t: Throwable) {
Log.e("API-GET-USERS", "Problem GETTING USERS", t) }
override fun onResponse(call: Call<List<Users>>, response: Response<List<Users>>) {
val result = UsersResult(response.body() ?: return run {
Log.e("API-ME", "Problem calling USERS")
})
peopleList = result
people_list.adapter = ManagePeopleAdapter(result)
}
}
d_manage_people.xml(对话资源文件)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp">
<TextView
android:id="@+id/manage_people_title"
android:gravity="center"
android:height="24dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/Subtitle1"
android:text="Create Item"
android:layout_marginBottom="16dp"
android:layout_gravity="center"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/people_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
这是我的错误
java.lang.IllegalStateException: people_list must not be null
顺便说一句,我使用的插件允许我不使用 findViewById
任何帮助将不胜感激:)
【问题讨论】:
-
这意味着
people_list不在Activity的布局中。 -
@EpicPandaForce 是的,它不是。我想知道是否有任何方法可以访问不在 Activity 布局中但在对话框布局中的视图
-
不是 inside Activity :p 但您可以做的是使用布局充气器来充气视图,然后使用
.setView()方法将其传递给对话框构建器, 然后你可以在你膨胀的视图上通过 id 找到视图 -
@EpicPandaForce 我会在回调中这样做吗?
-
@EpicPandaForce 因为当按下按钮时我已经在 onClick 方法上完成了所有这些操作
fun onPeopleClicked(view: View) { val dialog = AlertDialog.Builder(this) val view = layoutInflater.inflate(R.layout.d_manage_people, null) val userName = view.person_name view.manage_people_title.text = "Manage People" dataRetriever.getUsers(callbackGETUSERS, getAuth(), listID) dialog.setView(view) dialog.show() }格式很差的Yikes:/
标签: android kotlin android-recyclerview dialog adapter