gpt4 book ai didi

android - Kotlin Android 扩展访问 TextView

转载 作者:行者123 更新时间:2023-11-29 22:44:05 24 4
gpt4 key购买 nike

所以我正在自学 android 开发,我只是想用 Volley get 调用的结果更新 TextView 。我看过其他 stackoverflow 线程和 kotlin 教程,但似乎没有任何效果。

我正在处理 3 个主要文件

具有这些 View 的 activity_main.xml

<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="Title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />

<TextView
android:id="@+id/story"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:text="Story Contents"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/message" />

主要 Activity .kt

    import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import <GetRequest>

class MainActivity : AppCompatActivity() {

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {

val getRequestUtils = GetRequestUtils()
getRequestUtils.makeGetRequest(this)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
message.setText(R.string.title_dashboard)
return@OnNavigationItemSelectedListener true
}
}
false
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}}

GetRequestUtils.kt

import android.app.Activity
import android.content.Context
import android.os.Bundle
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.activity_main.*
import <Book>
import <R>

class GetRequestUtils : Activity(){
val parseResponseUtil = ParseResponseUtil()

fun makeGetRequest(context: Context) {
val queue = Volley.newRequestQueue(context)
val stringRequest = StringRequest(Request.Method.GET, <Get Request URL>, Response.Listener<String> { response ->
val book = parseResponseUtil.getBookComponents(response)
message?.text = book.title
story?.text = book.story
}, Response.ErrorListener { Book("Something went wrong", "Something went wrong", "Something went wrong")})
queue.add(stringRequest)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

**请注意,一些导入已替换为,因为导入中有我的名字。

每当我进行此调用时,我都会收到一条错误消息

Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

根据我的研究,这是因为在调用 get 时 View 尚未创建或无法访问,但是当我尝试在 GetRequestUtils 的 onCreate 中设置消息和故事 TextView 时,

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
message.setText("HERE")
story.setText("HERE2")
}

textViews 没有更新,所以我不确定我什至不能正确访问 activity_main.xml 上的 textViews。

谁能帮我看看我做错了什么?

最佳答案

您可以将每个 Activity 视为一个屏幕,因为您已将 R.layout.activity_main xml 文件附加到 MainActivity,因此您不应将其附加到其他任何地方(除非你想创建另一个具有相同布局的屏幕)。 GetRequestUtils 是一个用于获取数据的类,因此它不应扩展 Activity。从API获取数据后返回值,可以使用接口(interface)回调

    class GetRequestUtils {

interface Callback {
fun onDone(book: Book)
}

val parseResponseUtil = ParseResponseUtil()

fun makeGetRequest(context: Context, callback: Callback) {
val queue = Volley.newRequestQueue(context)
val stringRequest = StringRequest(Request.Method.GET, <Get Request URL>, Response.Listener<String> { response ->
val book = parseResponseUtil.getBookComponents(response)
callback.onDone(book)
}, Response.ErrorListener {
val book = Book("Something went wrong", "Something went wrong", "Something went wrong")
callback.onDone(book)
})
queue.add(stringRequest)
}
}

MainActivity中,从Request Util 获取回调时将值设置为TextView

R.id.navigation_home -> {
val getRequestUtils = GetRequestUtils()
getRequestUtils.makeGetRequest(this, object : GetRequestUtils.Callback {
override fun onDone(book: Book) {
message?.text = book.title
story?.text = book.story
})
return@OnNavigationItemSelectedListener true
}

关于android - Kotlin Android 扩展访问 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58722595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com