skip to Main Content
private fun getSmsManagerForSubscriptionId(context: Context, subsId: Int): SmsManager {
        val smsManager = if (Build.VERSION.SDK_INT >= M) {
            context.getSystemService(SmsManager::class.java) as SmsManager
        } else {
            TODO("VERSION.SDK_INT < M")
        }
        val smsManagerInstanceForSubsId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            smsManager.createForSubscriptionId(subsId)
        } else {
            TODO("VERSION.SDK_INT < S")
        }
        return smsManagerInstanceForSubsId
    }

I am using above code to create an smsManager Instance but the code is showing error at

context.getSystemService(SmsManager::class.java) as SmsManager

below is the error it is showing

Cannot cast null object to non-null object.

2

Answers


  1. I didn’t get any null exceptions when I ran it

    import android.content.Context
    import android.os.Build
    import android.os.Build.VERSION_CODES.M
    import android.os.Bundle
    import android.telephony.SmsManager
    import android.util.Log
    import android.widget.TextView
    import androidx.appcompat.app.AppCompatActivity
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val textView = TextView(applicationContext)
    
            getSmsManagerForSubscriptionId(applicationContext, 1)
            Log.d("smsManager1 ", "succeed")
            getSmsManagerForSubscriptionId(textView.context, 2)
            Log.d("smsManager2", "succeed")
    
        }
        
        private fun getSmsManagerForSubscriptionId(context: Context, subsId: Int): SmsManager {
            val smsManager = if (Build.VERSION.SDK_INT >= M) {
                context.getSystemService(SmsManager::class.java) as SmsManager
            } else {
                TODO("VERSION.SDK_INT < M")
            }
            val smsManagerInstanceForSubsId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                smsManager.createForSubscriptionId(subsId)
            } else {
                TODO("VERSION.SDK_INT < S")
            }
            return smsManagerInstanceForSubsId
        }
    
    }
    

    enter image description here

    enter image description here

    Login or Signup to reply.
  2. The error you’re encountering suggests that the getSystemService method is returning a null object, and you’re trying to cast it to a non-null SmsManager object. This can happen if the system service for SmsManager is not available or accessible in the given Context.

    To handle this situation, you can modify your code as follows:

    private fun getSmsManagerForSubscriptionId(context: Context, subsId: Int): SmsManager {
        val smsManager = if (Build.VERSION.SDK_INT >= M) {
            context.getSystemService(Context.SMS_SERVICE) as SmsManager
        } else {
            // Handle the case when SDK_INT < M
            TODO("VERSION.SDK_INT < M")
        }
        
        val smsManagerInstanceForSubsId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            smsManager.createForSubscriptionId(subsId)
        } else {
            // Handle the case when SDK_INT < S
            TODO("VERSION.SDK_INT < S")
        }
        
        return smsManagerInstanceForSubsId
    }
    

    Instead of using SmsManager::class.java, you can directly use Context.SMS_SERVICE as the argument to getSystemService(). This ensures that the system service for SMS is retrieved correctly, regardless of the Kotlin version you’re using.

    Make sure that you have the necessary permissions declared in your AndroidManifest.xml file to access the SMS functionality, such as <uses-permission android:name="android.permission.SEND_SMS" />.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search