user1157751
user1157751

Reputation: 2457

Data passing through intents with string returns null

I'm trying to pass a string and a few integers to another activity. The problem is that the integers can be passed successfully, but not the string value. I'm searched the forum for similar questions, but I was not able to find my problem.

Main Activity Class:

    public void onClick(View v)
    {
        Intent debug = new Intent("bluetooth_4.pack.USBDebugMode");

        if((port != null))
        {
            debug.putExtra("port", port.toString());
            debug.putExtra("buadRate", buadRate);
            debug.putExtra("dataBits", dataBits);
            debug.putExtra("stopBits", stopBits);
            debug.putExtra("parity", parity);

            startActivity(debug);

            Toast.makeText(MainActivity.this, "Switching to USB Debug Mode" , Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(MainActivity.this, "You haven't selected a device yet!" , Toast.LENGTH_SHORT).show();
        }
    }

New Activity Class:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.usb_debug_main);

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);

    button1.setOnClickListener(myButtonListener1);
    button2.setOnClickListener(myButtonListener2);

    ActionBar actionBar = getActionBar();
    actionBar.hide();

    port = getIntent().getStringExtra(port);
    buadRate = getIntent().getIntExtra("buadRate", 0);
    dataBits = getIntent().getIntExtra("dataBits", 0);
    stopBits = getIntent().getIntExtra("stopBits", 0);
    parity = getIntent().getDoubleExtra("parity", 0);

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

    TextView text = (TextView) findViewById(R.id.textView2);

    text.append("Port: " + port + "\r\n");
    text.append("Buad Rate: " + buadRate + "\r\n");
    text.append("Data Bits: " + dataBits + "\r\n");
    text.append("Stop Bits: " + stopBits + "\r\n");
    text.append("Parity: " + parity + "\r\n");
}

I have a text box, and all it does is that the displays some data, while parameters such as buad rate, data bits, stop bits, and parity has data, port always return null.

However, port must not be null in order to start the activity.

Upvotes: 0

Views: 192

Answers (2)

Shajeel Afzal
Shajeel Afzal

Reputation: 5953

You forget to pass the correct key in your NewActivityClass, i have a tip for you!

Always define public constants some where in your app. ( i usually create MyConstants.Java for that purpose. This is the only way in which you can be sure that you are passing the correct keys in both sending class and receiving class

Thanks.

Upvotes: 1

Jade Byfield
Jade Byfield

Reputation: 4816

Change this line

 port = getIntent().getStringExtra(port);

To

port = getIntent().getStringExtra("port");

Upvotes: 1

Related Questions