I'm facing following Errors/Exceptions when receiving data from one App to another App
in Kotlin (Android):
- Class Not Found When Unmarshalling: com.example.Parcelable.User
- mExtras=Bundle[{)}]
- mParcelledData=null
- mMap=size 0
Also, In MainActivtiy: if(intent.hasExtra("USER"))
statement giving error and displaying Toast message written in Else statement.
Please help me how I can resolve these exceptions ? Thanks a Lot!
(SendingApp File) -> MainActivity.kt
btnSendData1.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND).setType("text/plain")
intent.component =
ComponentName.unflattenFromString("com.example.receive")
val args = Bundle()
args.putParcelable(USER_KEY, User("Ahmer", "28"))
intent.putExtras(args)
Log.d("Ahmer", args.toString())
startActivity(Intent.createChooser(intent, "Share to..."))
}
(ReceivingApp File) -> MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.tv_username)
when (intent?.action) {
Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
handleSendText(intent)
} else {
Toast.makeText(this, "No Text", Toast.LENGTH_SHORT).show()
}
}
}
}
@SuppressLint("SetTextI18n")
private fun handleSendText(intent: Intent) {
if (intent.extras != null) {
if (intent.hasExtra("USER")) {
Log.d("Intent", intent.toString())
val bundle = intent.extras
val user = bundle?.getParcelable<User>("USER") as User
textView.text = "${user.name}${user.age}"
} else {
Toast.makeText(this, "Has Extras Null", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Extras Null", Toast.LENGTH_SHORT).show()
}
}
}
(ReceivingApp File) -> User.kt
package com.example.receive
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class User(
var name: String,
var age: String
) : Parcelable