I have searched through multiple posts on here which seem to all have the same problem but with different causes. I am getting NULL results from my bundle when calling them from the second activity. I will post more of my code if required;
Here is where i package my bundle on initial activity:
val extras = Bundle()
extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
intent.putExtras(extras)
And when I am in my second activity:
val bundle :Bundle ?=intent.extras
val difficultystring = bundle?.getString("EXTRA_DIFFICULTY")
val catstring = bundle?.getString("EXTRA_CAT")
txtV.text = "Your difficulty level is " + difficultystring + " and your cat is " + catstring
In both cases it return null. The spinners initialise on create so there should always be a value to call, can anyone point me in the right direction please?
FULL CODE (less functions - not causing any issues)
Activity 1
class MainActivity : AppCompatActivity() {
lateinit var btnSTART: Button
lateinit var switchSNOW: Switch
lateinit var imageviewSNOW: ImageView
lateinit var requested_difficulty: String
lateinit var dif_spinner: Spinner
lateinit var cat_spinner: Spinner
var difficulty_array = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dif_spinner = findViewById(R.id.difficulty_spinner)
cat_spinner = findViewById(R.id.cat_spinner)
switchSNOW = findViewById(R.id.snow_switch) as Switch
imageviewSNOW = findViewById(R.id.imageViewSnow) as ImageView
imageviewSNOW.setVisibility(View.INVISIBLE);
switchSNOW.setOnCheckedChangeListener { _, isChecked ->
if(isChecked){
imageviewSNOW.setVisibility(View.VISIBLE);
} else {
imageviewSNOW.setVisibility(View.INVISIBLE);
}
}
btnSTART = findViewById(R.id.buttonSTART) as Button
btnSTART.setOnClickListener{
val intent = Intent(this, Quiz::class.java)
startActivity(intent)
}
// Create an ArrayAdapter using the string array and a default spinner layout
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter.createFromResource(
this,
R.array.difficulty_array,
android.R.layout.simple_spinner_item
).also { adapter ->
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
dif_spinner.adapter = adapter
}
ArrayAdapter.createFromResource(
this,
R.array.cat_array,
android.R.layout.simple_spinner_item
).also { adapter ->
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
cat_spinner.adapter = adapter
}
val extras = Bundle()
extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
intent.putExtras(extras)
}
}
Activity 2
class Quiz : AppCompatActivity() {
lateinit var txtV: TextView
lateinit var btn1: Button
lateinit var btn2: Button
lateinit var btn3: Button
lateinit var btn4: Button
lateinit var viewQ: TextView
var ans: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)
btn1 = findViewById(R.id.button1) as Button
btn2 = findViewById(R.id.button2) as Button
btn3 = findViewById(R.id.button3) as Button
btn4 = findViewById(R.id.button4) as Button
viewQ = findViewById(R.id.textview_question)
txtV = findViewById(R.id.textView_info) as TextView
ans = GetQuestion(1, rand(1, 6))
btn1.setOnClickListener{
val theirans: String = btn1.getText().toString() //this will get a string
val TheirAnswer = theirans.toInt() //this will get a no from the string
if(TheirAnswer==ans) {
btn1.setBackgroundResource(R.color.lime_green);
} else {
btn1.setBackgroundResource(R.color.red);
}
next()
};
btn2.setOnClickListener {
val theirans: String = btn2.getText().toString() //this will get a string
val TheirAnswer = theirans.toInt() //this will get a no from the string
if(TheirAnswer==ans) {
btn2.setBackgroundResource(R.color.lime_green);
} else {
btn2.setBackgroundResource(R.color.red);
}
next()
};
btn3.setOnClickListener{
val theirans: String = btn3.getText().toString() //this will get a string
val TheirAnswer = theirans.toInt() //this will get a no from the string
if(TheirAnswer==ans) {
btn3.setBackgroundResource(R.color.lime_green);
} else {
btn3.setBackgroundResource(R.color.red);
}
next()
};
btn4.setOnClickListener{
val theirans: String = btn4.getText().toString() //this will get a string
val TheirAnswer = theirans.toInt() //this will get a no from the string
if(TheirAnswer==ans) {
btn4.setBackgroundResource(R.color.lime_green);
} else {
btn4.setBackgroundResource(R.color.red);
}
next()
};
val bundle :Bundle ?=intent.extras
val difficultystring = bundle?.getString("EXTRA_DIFFICULTY")
val catstring = bundle?.getString("EXTRA_CAT")
txtV.text = "Your difficulty level is " + difficultystring + " and your category is " + catstring
}
Question raised by @cutiko provided the answer to my problem. For some reason I added the bundle to the end of my code rather than with my start activity intent.
From activity 1 my code went from:
btnSTART.setOnClickListener{
val intent = Intent(this, Quiz::class.java)
startActivity(intent)
}
&
val extras = Bundle()
extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
intent.putExtras(extras)
to
btnSTART.setOnClickListener{
val intent = Intent(this, Quiz::class.java)
val extras = Bundle()
extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
intent.putExtras(extras)
startActivity(intent)
}