如何使用 Log.e() 记录值并检查 logcat 它们在 kotlin 中是否存在或为空?
How to use Log.e() to log the values and check in the logcat whether they exists or empty in kotlin?
我必须从一个 activity 移动到另一个 activity,同时传递 value.But 它不会移动到另一个 activity.As 结果我必须使用 Log.e() 检查 logcat 如果传递的值是空的或者 not.I 使用了代码 below.But 它显示了一些东西 strange.I 有红线,每次都显示我输入了一个项目以移动到下一个 activity.
我搜索了很多但什么也没找到 introduction.Kindly 帮我解决这个问题
列表适配器
class ListAdapter (val context: Context, val list : ArrayList<SmsData>): BaseAdapter(){
@SuppressLint("ViewHolder", "SimpleDateFormat")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.rowlayout,parent,false)
/*view.clickable.setOnClickListener {
val intent = Intent(this, MainActivity1::class.java)
startActivity(intent)
}*/
list[position].senderName?.let{
view.sender.text = it.substring(0,1).toUpperCase()
}
view.sms_sender.text = list[position].senderName
view.sms_message.text = list[position].message
view.sms_date.text = list[position].date.toString()
view.clickable.setOnClickListener { View ->
/*val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
Log.e("MainActivity1","show address")
context.startActivity(intent)*/
if(list.isNullOrEmpty() || !list.contains(position) || list[position].senderName == null) {
Log.e("MainActivity1","senderName was null or not found")
}else{
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}
}
return view
}
override fun getItem(position: Int): Any {
return list[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return list.size
}
}
预期:
在 logcat 中显示一些内容,这有助于我检测值是否通过。
实际
结果我得到了这些红线。
Log.e
用于错误记录,这就是它显示为红色的原因。使用 Log.d
进行调试或使用 Log.v
进行详细说明。
这是所有日志类型:https://developer.android.com/reference/android/util/Log
ASSERT Priority constant for the println method.
DEBUG Priority constant for the println method; use Log.d.
ERROR Priority constant for the println method; use Log.e.
INFO Priority constant for the println method; use Log.i.
VERBOSE Priority constant for the println method; use Log.v.
WARN Priority constant for the println method; use Log.w.
此外,你应该打印这个:
Log.e("MainActivity1",list[position].senderName)
或
Log.e("MainActivity1",list.size())
查看列表的大小或检查它是否为空
或者,您可以通过执行以下操作对此进行改进:
if(list.isNullOrEmpty() || !list.contains(position) || list[position].senderName == null) {
Log.e("MainActivity1","senderName was null or not found")
}else{
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}
在 OP 发布适配器 class 之后,我意识到它是一个模型的适配器,所以这会起作用:
if(list.isEmpty() || list[position].senderName == null) {
Log.e("MainActivity1","senderName was null or list empty")
} else {
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}
我必须从一个 activity 移动到另一个 activity,同时传递 value.But 它不会移动到另一个 activity.As 结果我必须使用 Log.e() 检查 logcat 如果传递的值是空的或者 not.I 使用了代码 below.But 它显示了一些东西 strange.I 有红线,每次都显示我输入了一个项目以移动到下一个 activity.
我搜索了很多但什么也没找到 introduction.Kindly 帮我解决这个问题
列表适配器
class ListAdapter (val context: Context, val list : ArrayList<SmsData>): BaseAdapter(){
@SuppressLint("ViewHolder", "SimpleDateFormat")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.rowlayout,parent,false)
/*view.clickable.setOnClickListener {
val intent = Intent(this, MainActivity1::class.java)
startActivity(intent)
}*/
list[position].senderName?.let{
view.sender.text = it.substring(0,1).toUpperCase()
}
view.sms_sender.text = list[position].senderName
view.sms_message.text = list[position].message
view.sms_date.text = list[position].date.toString()
view.clickable.setOnClickListener { View ->
/*val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
Log.e("MainActivity1","show address")
context.startActivity(intent)*/
if(list.isNullOrEmpty() || !list.contains(position) || list[position].senderName == null) {
Log.e("MainActivity1","senderName was null or not found")
}else{
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}
}
return view
}
override fun getItem(position: Int): Any {
return list[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return list.size
}
}
预期:
在 logcat 中显示一些内容,这有助于我检测值是否通过。
实际
结果我得到了这些红线。
Log.e
用于错误记录,这就是它显示为红色的原因。使用 Log.d
进行调试或使用 Log.v
进行详细说明。
这是所有日志类型:https://developer.android.com/reference/android/util/Log
ASSERT Priority constant for the println method.
DEBUG Priority constant for the println method; use Log.d.
ERROR Priority constant for the println method; use Log.e.
INFO Priority constant for the println method; use Log.i.
VERBOSE Priority constant for the println method; use Log.v.
WARN Priority constant for the println method; use Log.w.
此外,你应该打印这个:
Log.e("MainActivity1",list[position].senderName)
或
Log.e("MainActivity1",list.size())
查看列表的大小或检查它是否为空
或者,您可以通过执行以下操作对此进行改进:
if(list.isNullOrEmpty() || !list.contains(position) || list[position].senderName == null) {
Log.e("MainActivity1","senderName was null or not found")
}else{
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}
在 OP 发布适配器 class 之后,我意识到它是一个模型的适配器,所以这会起作用:
if(list.isEmpty() || list[position].senderName == null) {
Log.e("MainActivity1","senderName was null or list empty")
} else {
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}