I've just been coding in kotlin for a while. I've got some problems.
It always return null data in after I click item in second activity.
first activity
btnClick.setOnClickListener { v ->
val intent = Intent(applicationContext, NumberPickerActivity::class.java)
startActivityForResult(intent, 777)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
try {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 777 && resultCode == Activity.RESULT_OK) {
val result = data?.getStringExtra("picked_product").toString()
Toast.makeText(applicationContext, result, Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
}
}
second activity
override fun onItemClick(item: Product) {
val intent = Intent()
intent.putExtra("picked_product", item.price)
setResult(Activity.RESULT_OK, intent)
finish()
}
Because you are expecting an Int, do this instead:
val result = data?.getIntExtra("picked_product", 0) //0 will be used in case no value in data and result is now Integer.
The extra you put in your intent is an Integer (item.price). But you are trying to retrive a String data?.getStringExtra("picked_product").
Sinc the intent does not contain a String at the key "picked_product", it returns null.
You should try to get an Int extra :
val result = data?.getIntExtra("picked_product")
Nothing to do with your problem but it's useless to do
data?.getStringExtra("picked_product").toString()
Since it return you a String the use of toString() is useless
Related
Hi I am creating a SideActivity to gather some results and pass them back to Main Activity. However, the two strings value from the putextra are NULL rather than the string "20" and the jamSize "medium". Is there a way of passing the data properly?
Here in my Main Activity I have a setOnClickListener and a onActivityResult function.
jamButton.setOnClickListener {
var intent = Intent(this#MainActivity, SideActivity::class.java)
intent.putExtra("jamName", "raspberry")
intent.putExtra("jamPrice", "12.00")
startActivityForResult(intent, 1) // passing request code value 1
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK) {
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
val jamSize:String = intent.getStringExtra("jamSize").toString()
val newJam = DataModel("Jam", "$jamSize", "$jamPrice")
list.add(0, newJam)
jamAdapter.notifyItemInserted(0)
}
}
Here is my Second Activity
completeBtn.setOnClickListener {
val jamPrice: String = textView2.text.toString()
val jamSize: String = textView3.text.toString()
val intent = Intent(this#SideActivity, MainActivity::class.java)
intent.putExtra("jamPrice", "20.00")
intent.putExtra("jamSize", jamSize)
setResult(Activity.RESULT_OK, intent)
finish()
}
In your onActivityResult don't use this :
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
because the intent variable is the Intent of the activity instead use :
val jamPrice: String = data?.getStringExtra("jamPrice").toString()
Halllo, my BarcodeActivity is called by startActivityForResult through my PCActivity. The value of the scanned barcode should then be returned back to the PCActivity and inserted in a text field there. Unfortunately, I do not get a value back. However, the app does not crash either. Here is my code.
PCActivity:
sn_mb.setDrawableRightTouch {
val intent = Intent(this#PCActivity, BarcodeActivity::class.java)
startActivityForResult(intent, 1)
}
[...]
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val returnedSN = intent.getStringExtra("return_sn")
sn_mb.setText(returnedSN)
} else {
sn_mb.setText("FEHLER!")
}
}
BarcodeActivity
saveBtn.setOnClickListener {
val sn = editTextBarcode.text.toString()
sn.toString()
if (sn!= "") {
val returnIntent:Intent = Intent()
returnIntent.putExtra("return_sn", sn)
setResult(Activity.RESULT_OK, returnIntent)
finish()
} else {
Toast.makeText(
applicationContext,
"Das ist keine gültige Seriennummer",
Toast.LENGTH_SHORT
).show()
}
}
I hope someone can explain or help me with this problem. Thank you very much.
You are extracting your data from the wrong place.
Replace
val returnedSN = intent.getStringExtra("return_sn")
with
val returnedSN = data.getStringExtra("return_sn")
The problem is you are not passing the scanned bar code value (sn) to the intent(returnIntent) in your BarCodeActivity.
First make sure sn is String since you want to pass a StringExtra, therefore:
val sn = editTextBarcode.text.toString()
And then pass sn to your return intent:
returnIntent.putExtra("return_sn", sn)
Notice that in your code you are passing integer 1 instead of sn.
EDIT:
One more minor fix to your code, didn't notice it:
val returnIntent:Intent = getIntent()
Notice that it's getIntent() instead of Intent()
I am new to kotlin. I wrote this code that will get image from image picker.
But I am getting error on line:val filePath: Uri = attr.data.getData()
error: Unresolved reference. Is there any change in kotlin because this code was working properly in java (Means I migrated to kotlin)
And another error on imageStore(bitmap) error: smart cast to bitmap is impossible.
I've searched for documentation but couldn't solve this 2 problems .
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
val filePath: Uri = attr.data.getData()
try {
val inputStream: InputStream? = contentResolver.openInputStream(filePath)
bitmap = BitmapFactory.decodeStream(inputStream)
imageStore(bitmap)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
But I am getting error on line:val filePath: Uri = attr.data.getData() error: Unresolved reference
Well, this means the compiler can't understand what you're referring to. Is it correct to say this ?
Well, considering you don't have any object named attr it would seem that the compiler is correct.
this means you should remove the attr part, something like:
val filePath: Uri = data?.getData() ?: return
The return here will stop executing the rest of the method if the data from the intent is null
To resolve your second problem, you'll need something like this :
bitmap?.let { bitmapInstance ->
imageStore(bitmapInstance)
}
What does this do ?
Well, it gives you thread safe access to your bitmap object and also ensures that the instance isn't null.
As #a_local_nobody said "the migration tool isn't perfect", I learned the basics of kotlin and found that the following code works fine for me.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val filePath = data.data
try {
val inputStream = contentResolver.openInputStream(filePath!!)
bitmap = BitmapFactory.decodeStream(inputStream)
imageView!!.setImageBitmap(bitmap)
imageStore(bitmap)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
Replace this line val filePath: Uri = attr.data.getData() with
val filePath: Uri = data!!.getData()!!
Updated code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
val filePath: Uri = data!!.getData()!!
try {
val inputStream: InputStream? = contentResolver.openInputStream(filePath)
var bitmap = BitmapFactory.decodeStream(inputStream)
imageStore(bitmap)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
I am trying to get data from one activity in other. But data always null.
val res = Intent()
res.putExtra("uri", imageUri)
setResult(Activity.RESULT_OK, res)
finish()
In the second activity I always get data as null. What is the problem? (imageUri is not null when I put it).
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == Activity.RESULT_OK){
//data == null, WHY???
}
}
I think you need to define the intent with the current context and target class.
val intent = Intent(this#HomeActivity,ProfileActivity::class.java) // define you activity in which you want to go
intent.putExtra("Username","John Doe")
startActivity(intent)
Here's my startActivityForResult():
val initialPosition = frame.tag as Int
val currentPosition = constraint.indexOfChild(frame)
Log.d(TAG, "initial position: $initialPosition") // prints initial position: 2
Log.d(TAG, "current position: $currentPosition") // prints current position: 2
val galleryIntent = Intent().apply {
type = "image/*"
action = Intent.ACTION_PICK
putExtra("initialPosition", initialPosition)
putExtra("currentPosition", currentPosition)
setResult(5)
}
startActivityForResult(Intent.createChooser(galleryIntent, "Pick image"), PICK_IMAGE_REQUEST_CODE)
However, I'm unable to retrieve the data here:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK){
Log.d(TAG, "ResultCode: $resultCode SUCCESS") // prints -1 SUCCESS
}
when (requestCode){
PICK_IMAGE_REQUEST_CODE -> {
Log.d(TAG, "${data?.getIntExtra("initialPosition", 0)}") // prints 0
Log.d(TAG, "${data?.extras?.getInt("initialPosition")}") // prints 0
Log.d(TAG, "${data?.extras?.getInt("currentPosition")}") // prints 0
}
}
super.onActivityResult(requestCode, resultCode, data)
}
Any idea why?
setResult() will not set galleryIntent's result code. The one that will set that result is the activity that you picked during Intent.ACTION_PICK.
Assuming you picked the system image picker. It will return RESULT_OK(which is -1) if you select an image, and return RESULT_CANCELED(which is 0) if you backpress.
Note that you cannot change what other activity returns for resultCode if you use an Implicit Intent, which is what you just did.