我正在学习 Kotlin android 基础课程,我期待在一行简单的代码中进行成员声明,我不知道该怎么做
I am doing the Kotlin android basics course and I'm getting expecting member declaration on a simple line of code, I have no idea what to do
您好,我是 kotlin 的新手,我正在开发单词词典应用程序,我不小心删除了一行代码,但设法将其放回原处,但由于某种原因现在无法正常工作。错误是期待成员声明。我猜它链接到期望顶级声明的代码的第 118 行,它只是一个大括号。请帮忙
/**
* Adapter for the [RecyclerView] in [DetailActivity].
*/
class WordAdapter(private val letterId: String, context: Context) :
RecyclerView.Adapter<WordAdapter.WordViewHolder>() {
private val filteredWords: List<String>
init {
// Retrieve the list of words from res/values/arrays.xml
val words = context.resources.getStringArray(R.array.words).toList()
filteredWords = words
// Returns items in a collection if the conditional clause is true,
// in this case if an item starts with the given letter,
// ignoring UPPERCASE or lowercase.
.filter { it.startsWith(letterId, ignoreCase = true) }
// Returns a collection that it has shuffled in place
.shuffled()
// Returns the first n items as a [List]
.take(5)
// Returns a sorted version of that [List]
.sorted()
}
class WordViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val button = view.findViewById<Button>(R.id.button_item)
}
override fun getItemCount(): Int = filteredWords.size
/**
* Creates new views with R.layout.item_view as its template
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
val layout = LayoutInflater
.from(parent.context)
.inflate(R.layout.item_view, parent, false)
// Setup custom accessibility delegate to set the text read
layout.accessibilityDelegate = Accessibility
return WordViewHolder(layout)
}
/**
* Replaces the content of an existing view with new data
*/
override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
holder.button.setOnClickListener {
val queryUrl: Uri = **Uri.parse("${DetailActivity.SEARCH_PREFIX}${item}")<---Unresolved reference item**
val intent = Intent(Intent.ACTION_VIEW, queryUrl)
**context.startActivity(intent)<---Unresolved reference context**
}
}
val item = filteredWords[position]<--Unresolved reference position
// Needed to call startActivity
val context = holder.view.context <--Unresolved reference holder
// Set the text of the WordViewHolder
**holder.button.text = item** **<---This line is the error Expecting member declaration**
}
// Setup custom accessibility delegate to set the text read with
// an accessibility service
companion object Accessibility : View.AccessibilityDelegate() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onInitializeAccessibilityNodeInfo(
host: View?,
info: AccessibilityNodeInfo?
) {
super.onInitializeAccessibilityNodeInfo(host, info)
// With `null` as the second argument to [AccessibilityAction], the
// accessibility service announces "double tap to activate".
// If a custom string is provided,
// it announces "double tap to <custom string>".
val customString = host?.context?.getString(R.string.look_up_word)
val customClick =
AccessibilityNodeInfo.AccessibilityAction(
AccessibilityNodeInfo.ACTION_CLICK,
customString
)
info?.addAction(customClick)
}
}
} <------Here is line 118 Expecting a top level declaration
删除 val 项目声明顶部的“}”
您好,我是 kotlin 的新手,我正在开发单词词典应用程序,我不小心删除了一行代码,但设法将其放回原处,但由于某种原因现在无法正常工作。错误是期待成员声明。我猜它链接到期望顶级声明的代码的第 118 行,它只是一个大括号。请帮忙
/**
* Adapter for the [RecyclerView] in [DetailActivity].
*/
class WordAdapter(private val letterId: String, context: Context) :
RecyclerView.Adapter<WordAdapter.WordViewHolder>() {
private val filteredWords: List<String>
init {
// Retrieve the list of words from res/values/arrays.xml
val words = context.resources.getStringArray(R.array.words).toList()
filteredWords = words
// Returns items in a collection if the conditional clause is true,
// in this case if an item starts with the given letter,
// ignoring UPPERCASE or lowercase.
.filter { it.startsWith(letterId, ignoreCase = true) }
// Returns a collection that it has shuffled in place
.shuffled()
// Returns the first n items as a [List]
.take(5)
// Returns a sorted version of that [List]
.sorted()
}
class WordViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val button = view.findViewById<Button>(R.id.button_item)
}
override fun getItemCount(): Int = filteredWords.size
/**
* Creates new views with R.layout.item_view as its template
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
val layout = LayoutInflater
.from(parent.context)
.inflate(R.layout.item_view, parent, false)
// Setup custom accessibility delegate to set the text read
layout.accessibilityDelegate = Accessibility
return WordViewHolder(layout)
}
/**
* Replaces the content of an existing view with new data
*/
override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
holder.button.setOnClickListener {
val queryUrl: Uri = **Uri.parse("${DetailActivity.SEARCH_PREFIX}${item}")<---Unresolved reference item**
val intent = Intent(Intent.ACTION_VIEW, queryUrl)
**context.startActivity(intent)<---Unresolved reference context**
}
}
val item = filteredWords[position]<--Unresolved reference position
// Needed to call startActivity
val context = holder.view.context <--Unresolved reference holder
// Set the text of the WordViewHolder
**holder.button.text = item** **<---This line is the error Expecting member declaration**
}
// Setup custom accessibility delegate to set the text read with
// an accessibility service
companion object Accessibility : View.AccessibilityDelegate() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onInitializeAccessibilityNodeInfo(
host: View?,
info: AccessibilityNodeInfo?
) {
super.onInitializeAccessibilityNodeInfo(host, info)
// With `null` as the second argument to [AccessibilityAction], the
// accessibility service announces "double tap to activate".
// If a custom string is provided,
// it announces "double tap to <custom string>".
val customString = host?.context?.getString(R.string.look_up_word)
val customClick =
AccessibilityNodeInfo.AccessibilityAction(
AccessibilityNodeInfo.ACTION_CLICK,
customString
)
info?.addAction(customClick)
}
}
} <------Here is line 118 Expecting a top level declaration
删除 val 项目声明顶部的“}”