我试图将从微调器中选择的值传递给另一个类,但它传递null。
protected int secondsToUse;
final String titles[] = {
"1 Second","2 Seconds", "3 Seconds","4 Seconds",
"5 Seconds","6 Seconds","7 Seconds","8 Seconds","9 Seconds","10 Seconds"
};
mSeconds = (Spinner) view.findViewById(R.id.secondsSpinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, titles);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSeconds.setAdapter(arrayAdapter);
mSeconds.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// secondsToUse is declared a as int variable above
secondsToUse = position + 1;
}
// passing the value to another class.// PASSES AS NULL
Intent recipientsIntent = new Intent(getActivity(), RecipientsActivity.class);
recipientsIntent.putExtra("key1", secondsToUse);
startActivity(recipientsIntent);
// retrieving the value //retrieves as null
String Seconds = getIntent().getStringExtra("key1");
byte[] secs = Seconds.getBytes();
我试图获取用户在微调器上选择的位置,以便将其传递到计时器中以使图像自毁。此刻我空了
编辑:我忘了考虑到在我的第一个答案中getIntExtra()
有两个参数。它需要一个标识“键”和一个默认值。
要在中检索所选行的位置RecipientsActivity
,请保持您的代码不变。然后,在onCreate()
的RecipientsActivity
:
int seconds = getIntent().getIntExtra("key1", -1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句