【发布时间】:2020-11-18 12:09:11
【问题描述】:
我在这里搜索了多个帖子,这些帖子似乎都有相同的问题,但原因不同。从第二个活动调用它们时,我从我的包中得到 NULL 结果。如果需要,我会发布更多我的代码;
这是我在初始活动中打包我的捆绑包的地方:
val extras = Bundle()
extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
intent.putExtras(extras)
当我进行第二次活动时:
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
在这两种情况下,它都返回 null。微调器在创建时初始化,所以应该总是有一个值可以调用,有人能指出我正确的方向吗?
完整代码(更少的功能 - 不会导致任何问题)
活动一
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)
}
}
活动 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
}
【问题讨论】:
-
在你的第二个代码 sn-p 中,
bundle来自哪里? -
The spinners initialise on create so there should always be a value to call你知道这个事实吗?你记录/调试了吗? -
@CommonsWare 我在上面编辑了我的代码。复制到这里时错过了一行对不起
-
将
dif_spinner.getSelectedItem().toString()放入Log并跟踪它们 -
我们能看到
startActivity(intent)吗?
标签: android kotlin android-intent bundle