我正在开发一个新的应用程序,当用户单击图标时我想中断活动,但是当单击图标应用程序崩溃时
在我的Logcat错误下面
Process: com.empowered.healo, PID: 19079
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.empowered.healo/com.empowered.healo.ui.screens.therapy.TherapyInformationFragment}: java.lang.ClassCastException: com.empowered.healo.ui.screens.therapy.TherapyInformationFragment cannot be cast to android.app.Activity
图标onclick代码下方
if(it.key.endsWith("_rf")) {
attentionIcon.setColorFilter(ContextCompat.getColor(context!!, R.color.therapyInfoRedFlag))
attentionIcon.setOnClickListener {
val intent = Intent(context, TherapyInformationFragment::class.java)
startActivity(intent);
}
} else if(it.key.endsWith("_yf")) {
attentionIcon.setImageResource(R.drawable.ic_therapy_info_yellow_flag_attention)
attentionIcon.setColorFilter(ContextCompat.getColor(context!!, R.color.therapyInfoYellowWhiteFlag))
attentionIcon.setOnClickListener {
val intent = Intent(context!!, TherapyInformationFragment::class.java)
startActivity(intent);
}
}else{
attentionIcon.setImageResource(R.drawable.ic_therapy_info_yellow_flag_attention)
attentionIcon.setColorFilter(ContextCompat.getColor(context!!, R.color.therapyInfoYellowWhiteFlag))
attentionIcon.setOnClickListener {
val intent = Intent(context, TherapyInformationFragment::class.java)
startActivity(intent);
}
}
在我更新的代码下面,请给我建议
if(it.key.endsWith("_rf")) {
attentionIcon.setColorFilter(ContextCompat.getColor(context!!, R.color.therapyInfoRedFlag))
attentionIcon.setOnClickListener {
val newFragment = TherapyInformationFragment()
val transaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.therapy_fragment, newFragment)
transaction.addToBackStack(null)
transaction.commit()
}
} else if(it.key.endsWith("_yf")) {
attentionIcon.setImageResource(R.drawable.ic_therapy_info_yellow_flag_attention)
attentionIcon.setColorFilter(ContextCompat.getColor(context!!, R.color.therapyInfoYellowWhiteFlag))
attentionIcon.setOnClickListener {
val newFragment = TherapyInformationFragment()
val transaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.therapy_fragment, newFragment)
transaction.addToBackStack(null)
transaction.commit()
}
}else{
attentionIcon.setImageResource(R.drawable.ic_therapy_info_yellow_flag_attention)
attentionIcon.setColorFilter(ContextCompat.getColor(context!!, R.color.therapyInfoYellowWhiteFlag))
attentionIcon.setOnClickListener {
val newFragment = TherapyInformationFragment()
val transaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.therapy_fragment, newFragment)
transaction.addToBackStack(null)
transaction.commit()
}