1

Estoy tratando de pasar una variable string de una activity a otra activity sin éxito, estos son mis datos:

Dentro de Activity1 tengo este String: stringInfoUSB y este intent

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
.
.
final UsbDevice device = driver.getDevice();
final String stringInfoUSB = String.format("Driver: %s  Vendor: %s Product: %s",driver.getClass().getSimpleName(),
                HexDump.toHexString((short) device.getVendorId()),
                HexDump.toHexString((short) device.getProductId()));
.
.
.
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.nav_usb) {
        Intent intent = new Intent(this, UsbInfoActivity.class);
        intent.putExtra("infoUSB", stringInfoUSB);
        startActivity(intent);
    }

Este es la actividad2 que recibe la variable:

public class UsbInfoActivity extends AppCompatActivity {
private Button mBotonCancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_usb_info );

    Bundle bundle = getIntent().getExtras();                        
    String textoImportado1=bundle.getString("Info_USB");

    mBotonCancel = (Button)findViewById( R.id.bt2_SendButton ) ;

    String bufferTexto2 = textoImportado1;
    TextView mDumpTextView = (TextView)findViewById(R.id.tv2_ReadValues);
    mDumpTextView.setText(bufferTexto2);

    mBotonCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent2 = new Intent(v.getContext(), MainActivity.class);
            startActivityForResult(intent2, 0);
        }
    });

}

necesito enviar este string a la activity2 llamada UsbInfoActivity, despues de seleccionar un item de una barra de navegación, pero stringinfoUSB me da ERROR!, alguien me podria decir cual error?

4
  • Donde te da el error? al hacer el putExtra o en la otra clase? y un poco mas especifico con el error por favor el 15 nov. 2018 a las 23:45
  • Me da error al hacer putExtra, el compilador me dice que no reconoce la variable stringInfoUSB
    – W1ll
    el 16 nov. 2018 a las 0:13
  • Podrias mostrar el codigo del UsbInfoActivity, seguro no estas obteniendo el string debidamente :D
    – Carlosgub
    el 16 nov. 2018 a las 14:24
  • @W1ll declara únicamente la variable private String stringInfoUSB ; y la evaluas en el método onNavigationItemSelected() o en onCreate().
    – Jorgesys
    el 16 nov. 2018 a las 20:31

2 respuestas 2

1

Primeramente tienes un error al enviar el texto, estas escribiendo incorrectamente la llave:

 intent.putExtra("infoUSB", stringInfoUSB);

y lo tratas de recibir

String textoImportado1=bundle.getString("Info_USB");

debes recibirlo con la misma key que lo envías:

String textoImportado1=bundle.getString("info_USB");

Segundo, te recomiendo definir las variables, pero evaluar en onCreate() o en este caso en onNavigationItemSelected() sus valores:

private UsbDevice device;
private String stringInfoUSB;
...
...  
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    device = driver.getDevice();
    stringInfoUSB = String.format("Driver: %s  Vendor: %s Product: %s",driver.getClass().getSimpleName(),
                HexDump.toHexString((short) device.getVendorId()),
                HexDump.toHexString((short) device.getProductId()));

    int id = item.getItemId();

    if (id == R.id.nav_usb) {
        Intent intent = new Intent(this, UsbInfoActivity.class);
        intent.putExtra("infoUSB", stringInfoUSB);
        startActivity(intent);
    }
0

El detalle esta en que la variable que intentas enviar "infoUSB" no es la misma a la que intentas recuperar "Info_USB", ambas deben ser la misma variable en tu primer Activity tienes intent.putExtra("infoUSB", stringInfoUSB); por lo tanto en tu segundo Activity debe ser la misma String textoImportado1=bundle.getString("infoUSB");

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.