使用 android studio 时,我必须在我的 kotlin 代码中添加 try/catch,但不明白如何添加。下面是我必须添加 try/catch 的代码。
我还没有尝试任何东西,因为我完全困惑在哪里应用 try/catch。
1.
class SmsReceiver : BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent) {
val extras = intent.extras
if(extras != null){
val sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices){
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
SmsMessage.createFromPdu(sms[i] as ByteArray,format)
}else{
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context,"$phoneNumber:(Private)\n" + "messageText: $messageText",Toast.LENGTH_SHORT).show()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
2.
class MainActivity :AppCompatActivity(){
private val requestReceiveSms: Int = 3
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.RECEIVE_SMS) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.RECEIVE_SMS),
requestReceiveSms)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我原以为“短信收到 Toast 消息”,但我收到“不幸的是,应用程序已停止”并崩溃了..
try {
if(extras != null){
val sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices){
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
SmsMessage.createFromPdu(sms[i] as ByteArray,format)
}else{
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context,"$phoneNumber:(Private)\n" + "messageText: $messageText",Toast.LENGTH_SHORT).show()
}
}
}catch (ex:Exception){
//your error handling code here
//here, consider adding Log.e("SmsReceiver", ex.localizedMessage)
//this log statement simply prints errors to your android studio terminal and will help with debugging, alternatively leave it out
if (context != null){
Toast.makeText(context!!,ex.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
Run Code Online (Sandbox Code Playgroud)
您应该对可能引发异常的代码应用 try catch。对于您发布的代码,有几个地方可能会崩溃,例如索引越界 ( sms[i]) 或 if ( extras.getString("pdus") 无法找到此键,因此我的解决方案将这两个都包含在同一个 try catch 中,您需要什么然后做例外取决于你。
如果你想处理更具体的异常,你也可以这样做:
try {
if(extras != null){
val sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices){
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
SmsMessage.createFromPdu(sms[i] as ByteArray,format)
}else{
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context,"$phoneNumber:(Private)\n" + "messageText: $messageText",Toast.LENGTH_SHORT).show()
}
}
}catch (indexOutOfBoundsException:IndexOutOfBoundsException){
//your error handling code here
} catch (nullpointer : NullPointerException){
//your error handling code here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3095 次 |
| 最近记录: |