2

I'm beginning android developpement and i don't why my code is not working. The aim is simple : I have a main activity, a menu and a second activity. I want to send a float value from the main to the second activity and .. it's not working !

Here is my code from the main :

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_about:
        Intent intent = new Intent(MainActivity.this, AboutActivity.class);
        startActivity(intent);

        return true;
    case R.id.menu_home:

        return true;
    case R.id.menu_settings:        
        intent = new Intent(MainActivity.this, SettingsActivity.class);
        startActivity(intent);

        return true;
    case R.id.menu_battstat:
        intent = new Intent(MainActivity.this, StatsActivity.class);
        intent.putExtra("consumOn", 4);
        intent.putExtra("consumOff", 5);
        startActivity(intent);

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

}

And the StatsActivity.class

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stats_main);

    Intent intent = getIntent();

    float consumOn  = intent.getFloatExtra("consumOn", 0);
    float consumOff = intent.getFloatExtra("consumOff", 0);

    EditText editTextA = (EditText)findViewById(R.id.editText1);
    editTextA.setText(String.valueOf(consumOn), TextView.BufferType.EDITABLE);

    EditText editTextB = (EditText)findViewById(R.id.editText2);
    editTextB.setText(String.valueOf(consumOff), TextView.BufferType.EDITABLE);
} 

When I launch my code, it stills 0 and not a 4 and 5. Any ideas ? Thx.

Pierre
  • 31
  • 1
  • 5

4 Answers4

5

Replace the getFloatExtra with

Bundle bundle = getIntent().getExtras();
float yourFloat = bundle.getFloat("key");
mapodev
  • 988
  • 8
  • 14
  • 1
    Bundle bundle = getIntent().getExtras(); float consumOn = bundle.getFloat("consumOn"); float consumOff = bundle.getFloat("consumOff"); And it's not working too ... – Pierre Jun 30 '14 at 14:13
  • have you tried logging intent.hasExtra("consumOn") to see if it is actually getting passed? – erik Jun 30 '14 at 14:18
2

I'm going to go out on a limb here and say that maybe the putExtra is being interpreted as int rather than a float so it cannot find it. Try replacing these lines

    intent.putExtra("consumOn", 4);
    intent.putExtra("consumOff", 5);

with

    intent.putExtra("consumOn", 4f);
    intent.putExtra("consumOff", 5f);

because you haven't actually defined their type anywhere and they aren't variables.

Unknownweirdo
  • 485
  • 6
  • 16
0

You should pass bundle with your floats to intent and only then extract them

dimak
  • 36
  • 4
0

I am actually surprised that your intent is launching at all.. it seems like you only initialize the intent in the first case try moving Intent declaration out of the switch

public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {

     case R.id.menu_about:
        intent = new Intent(MainActivity.this, AboutActivity.class);
        startActivity(intent);
        return true;
    case R.id.menu_home:
        return true;
    case R.id.menu_settings:        
        intent = new Intent(MainActivity.this, SettingsActivity.class);
        startActivity(intent);
        return true;
    case R.id.menu_battstat:
        intent = new Intent(MainActivity.this, StatsActivity.class);
        intent.putExtra("consumOn", 4f);
        intent.putExtra("consumOff", 5f);
        startActivity(intent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
erik
  • 4,946
  • 13
  • 70
  • 120