0
我想使用此代碼從一個活動傳遞一些值到另一個:傳遞捆,每一個鍵爲null
Bundle bundle = new Bundle();
bundle.putInt("x_element",xElementBar.getProgress());
bundle.putInt("y_element",yElementBar.getProgress());
bundle.putInt("frames",framesBar.getProgress());
bundle.putInt("radioID", radioGroup.getCheckedRadioButtonId());
bundle.putBoolean("sound", soundCheck.isChecked());
bundle.putBoolean("vibrate",vibrateCheck.isChecked());
Intent intent = new Intent(MainActivity.this,GameActivity.class);
intent.putExtras(bundle);
startActivity(intent);
並使用該代碼的另一項活動顯示值:
Bundle bundle = getIntent().getExtras();
columnsText.setText("El número de columnas es: " + bundle.getString("x_element"));
rowText.setText("El número de filas es: " + bundle.getString("y_element"));
optionsText.setText("El número de opciones es: " + bundle.getString("frames"));
styleText.setText("Se mostrarán: " + (bundle.getInt("radioID") == R.id.radioNumbers?"números":"colores"));
soundText.setText("El sonido esta: " + (bundle.getBoolean("sound")?"activado":"desactivado"));
vibrateText.setText("La vibración esta: " + (bundle.getBoolean("vibrate")?"activada":"desactivada"));
但是,每個bundle.getX(ID)都會返回一個null,並與R.id.radioNumbers進行比較並在其餘顯示null。
TextViews(columnsText等)在第二個活動上創建包之前被聲明和鏈接。