在 Android 上传递了捆绑包,每个键都为空

Passed bundle on Android, every key is null

我正在尝试使用以下代码将一些值从 activity 传递给另一个:

    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);

并使用此代码在另一个 activity 上显示值:

    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) returns 都是一个 null,在与 R.id.radioNumbers 比较时崩溃并在其余部分显示 null。

在第二个 activity 上创建捆绑包之前声明并链接 TextView(columnsText 等)。

这是因为您将字段保存为一种类型并将它们检索为另一种类型:

发件人:

bundle.putInt("x_element",xElementBar.getProgress());

收件人:

columnsText.setText("El número de columnas es: " + bundle.getString("x_element"));

类型必须匹配,所以将接收器更改为bundle.getInt("x_element")以获取数据然后将其转换为String