另一个 Activity / 函数中的 Kotlin getString
Kotlin getString in another Activity / Function
当我使用这段代码时,我的回收器视图应用程序崩溃了,因为我得到了一个空对象引用异常 bt,当时我不知道如何修复它。我试图对我的视图文本中的字符串进行软编码,但它不起作用。当我对其进行硬编码时,它可以工作:(
问题在
行
mContext.getString(R.string.strClick)
这是我的代码:
package de.reuter.myapplication
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import de.reuter.timetable.RecycleAdapter
import kotlinx.android.synthetic.main.activity_content_backend.*
class BackendContentActivity : AppCompatActivity()
{
val mContext: Context = this
private val exampleList = generateDummyList(5, this)
private val adapter = RecycleAdapter(exampleList, mContext)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content_backend)
recycler_view.adapter = adapter
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.setHasFixedSize(true)
}
private fun generateDummyList(size: Int, mContext: Context): ArrayList<AdapterItems> {
val list = ArrayList<AdapterItems>()
var drawable: Int
var txt0: String
var txt1: String
var txt2: String = mContext.getString(R.string.strClick)
for (i in 0 until size) {
when (i % 5) {
0 -> {
drawable = R.drawable.bg_circle_blue
txt0 = "M"
txt1 = "Monday"
}
1 -> {
drawable = R.drawable.bg_circle_green
txt0 = "T"
txt1 = "Tuesday"
}
2 -> {
drawable = R.drawable.bg_circle_red
txt0 = "W"
txt1 = "Wednesay"
}
3 -> {
drawable = R.drawable.bg_circle_yellow
txt0 = "T"
txt1 = "Thursday"
}
4 -> {
drawable = R.drawable.bg_circle_purple
txt0 = "F"
txt1 = "Friday"
}
else -> {
drawable = R.drawable.bg_circle_purple
txt0 = "W"
txt1 = "Wrong"
}
}
var item = AdapterItems(drawable, txt0, txt1, txt2)
list += item
}
return list
}
}
我的回收适配器:
package de.reuter.timetable
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import de.reuter.myapplication.AdapterItems
import de.reuter.myapplication.MondayActivity
import de.reuter.myapplication.R
import kotlinx.android.synthetic.main.card_layout.view.*
class RecycleAdapter(private val exampleList: List<AdapterItems>, context: Context) :
RecyclerView.Adapter<RecycleAdapter.ExampleViewHolder>() {
var mContext = context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.card_layout,
parent, false
)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textView0.text = currentItem.text1
holder.textView1.text = currentItem.text2
holder.textView2.text = currentItem.text3
}
override fun getItemCount() = exampleList.size
inner class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageView: ImageView = itemView.image_view
var textView0: TextView = itemView.txtShortLetterM
var textView1: TextView = itemView.txtViewMonday
var textView2: TextView = itemView.txtClickLink
init {
imageView = itemView.findViewById(R.id.image_view)
textView0 = itemView.findViewById(R.id.txtShortLetterM)
textView1 = itemView.findViewById(R.id.txtViewMonday)
textView2 = itemView.findViewById(R.id.txtClickLink)
itemView.setOnClickListener { v: View ->
var position: Int = getAdapterPosition()
var text: String
when (position) {
0 -> {
var intent = Intent(mContext, MondayActivity::class.java)
mContext.startActivity(intent)
}
else -> {
text = "Click detected on item $position"
Snackbar.make(
v, text,
Snackbar.LENGTH_LONG
).setAction("Action", null).show()
}
}
}
}
}
}
非常感谢您的帮助
尝试将 generateDummyList(5, this)
调用移动到 BackendContentActivity
的 onCreate
方法 class:
class BackendContentActivity : AppCompatActivity() {
private lateinit var exampleList: ArrayList<AdapterItems>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content_backend)
exampleList = generateDummyList(5, this)
// ...
}
// ...
}
当我使用这段代码时,我的回收器视图应用程序崩溃了,因为我得到了一个空对象引用异常 bt,当时我不知道如何修复它。我试图对我的视图文本中的字符串进行软编码,但它不起作用。当我对其进行硬编码时,它可以工作:( 问题在
行mContext.getString(R.string.strClick)
这是我的代码:
package de.reuter.myapplication
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import de.reuter.timetable.RecycleAdapter
import kotlinx.android.synthetic.main.activity_content_backend.*
class BackendContentActivity : AppCompatActivity()
{
val mContext: Context = this
private val exampleList = generateDummyList(5, this)
private val adapter = RecycleAdapter(exampleList, mContext)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content_backend)
recycler_view.adapter = adapter
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.setHasFixedSize(true)
}
private fun generateDummyList(size: Int, mContext: Context): ArrayList<AdapterItems> {
val list = ArrayList<AdapterItems>()
var drawable: Int
var txt0: String
var txt1: String
var txt2: String = mContext.getString(R.string.strClick)
for (i in 0 until size) {
when (i % 5) {
0 -> {
drawable = R.drawable.bg_circle_blue
txt0 = "M"
txt1 = "Monday"
}
1 -> {
drawable = R.drawable.bg_circle_green
txt0 = "T"
txt1 = "Tuesday"
}
2 -> {
drawable = R.drawable.bg_circle_red
txt0 = "W"
txt1 = "Wednesay"
}
3 -> {
drawable = R.drawable.bg_circle_yellow
txt0 = "T"
txt1 = "Thursday"
}
4 -> {
drawable = R.drawable.bg_circle_purple
txt0 = "F"
txt1 = "Friday"
}
else -> {
drawable = R.drawable.bg_circle_purple
txt0 = "W"
txt1 = "Wrong"
}
}
var item = AdapterItems(drawable, txt0, txt1, txt2)
list += item
}
return list
}
}
我的回收适配器:
package de.reuter.timetable
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import de.reuter.myapplication.AdapterItems
import de.reuter.myapplication.MondayActivity
import de.reuter.myapplication.R
import kotlinx.android.synthetic.main.card_layout.view.*
class RecycleAdapter(private val exampleList: List<AdapterItems>, context: Context) :
RecyclerView.Adapter<RecycleAdapter.ExampleViewHolder>() {
var mContext = context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.card_layout,
parent, false
)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textView0.text = currentItem.text1
holder.textView1.text = currentItem.text2
holder.textView2.text = currentItem.text3
}
override fun getItemCount() = exampleList.size
inner class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageView: ImageView = itemView.image_view
var textView0: TextView = itemView.txtShortLetterM
var textView1: TextView = itemView.txtViewMonday
var textView2: TextView = itemView.txtClickLink
init {
imageView = itemView.findViewById(R.id.image_view)
textView0 = itemView.findViewById(R.id.txtShortLetterM)
textView1 = itemView.findViewById(R.id.txtViewMonday)
textView2 = itemView.findViewById(R.id.txtClickLink)
itemView.setOnClickListener { v: View ->
var position: Int = getAdapterPosition()
var text: String
when (position) {
0 -> {
var intent = Intent(mContext, MondayActivity::class.java)
mContext.startActivity(intent)
}
else -> {
text = "Click detected on item $position"
Snackbar.make(
v, text,
Snackbar.LENGTH_LONG
).setAction("Action", null).show()
}
}
}
}
}
}
非常感谢您的帮助
尝试将 generateDummyList(5, this)
调用移动到 BackendContentActivity
的 onCreate
方法 class:
class BackendContentActivity : AppCompatActivity() {
private lateinit var exampleList: ArrayList<AdapterItems>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content_backend)
exampleList = generateDummyList(5, this)
// ...
}
// ...
}