I am trying to pass a string variable from one activity to another activity without success, this is my data:
Inside Activity1 I have this String: stringInfoUSB and this 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);
}
This is the activity2 that receives the 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);
}
});
}
I need to send this string to the activity2 called UsbInfoActivity, after selecting an item from a navigation bar, but stringinfoUSB gives me ERROR!, can someone tell me what error?
First you have an error when sending the text, you are writing the key incorrectly:
and you try to receive it
you must receive it with the same key that you send it:
Second, I recommend you define the variables, but evaluate to
onCreate()
or in this case ononNavigationItemSelected()
their values:The detail is that the variable you are trying to send "infoUSB" is not the same as the one you are trying to retrieve "Info_USB", both must be the same variable in your first Activity you have intent.putExtra( "infoUSB" , stringInfoUSB); therefore in your second Activity it must be the same String textoImportado1=bundle.getString( "infoUSB" );