0

this is my first activity, i want to pass the value to second activity to do calculation and show the result in the textview.But I dont know why when i key in all the detail at activity 1, and click the custom button it jump out from the app

 public void onClick(View v){
        switch (v.getId())
        {
    case R.id.customMarco:

                if(gender.getText().toString().equals("") || age.getText().toString().equals("")||
                    height.getText().toString().equals("")||weight.getText().toString().equals("")||
                    activity.getText().toString().equals("")||goal.getText().toString().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Please key in all your detail above !", Toast.LENGTH_LONG).show();
                }
                else {
                    String cal = finalCal_result.getText().toString();
                    String carb = carb_result.getText().toString();
                    String protein = protein_result.getText().toString();
                    String fat = fat_result.getText().toString();

                    Intent intent = new Intent(this, CustomMarco.class);
                    intent.putExtra("Cal", cal);
                    intent.putExtra("Carb", carb);
                    intent.putExtra("Protein", protein);
                        intent.putExtra("Fat", fat);
                        startActivity(intent);
                    }
                    break;
}

This is my second activity, to get the valur from activity 1 and do calculation and show the result in textview cp. But I dont know why when i key in all the detail at activity 1, and click the custom button it jump out from the app

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

          cal = (EditText) findViewById(R.id.field_cal);
          String calNum =getIntent().getStringExtra("Cal");
          cal.setText ( calNum );
          cal.setOnClickListener(this);

          carb = (EditText) findViewById(R.id.field_carb);
          String carbNum =getIntent().getStringExtra("Carb");
          carb.setText ( carbNum );
          carb.setOnClickListener(this);

          Button setButton = (Button) findViewById(R.id.setButton);
          setButton.setOnClickListener(this);

          TextView cp = (TextView) findViewById(R.id.cp);

          float cal_n = Float.parseFloat(calNum);
          float carb_n = Float.parseFloat(carbNum);

          int carbPValue = calculateCP(cal_n,carb_n);
          cp.setText(String.valueOf(carbPValue) + " %");
        }

        private int calculateCP(float cal_n, float carb_n){
            return (int) ((carb_n/cal_n)*4*100);
        }

here the log cat error

12-03 21:25:39.284 
28640-28640/com.example.sam.iifym E/AndroidRuntime: 
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sam.iifym/com.example.sam.iifym.CustomMarco}: java.lang.NumberFormatException: Invalid int: "3809 cal"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2383)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435)
at android.app.ActivityThread.access$600(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5434)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: "3809 cal"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:375)
at java.lang.Integer.parseInt(Integer.java:366)
at java.lang.Integer.parseInt(Integer.java:332)
at com.example.sam.iifym.CustomMarco.onCreate(CustomMarco.java:59)
at android.app.Activity.performCreate(Activity.java:5224)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2347)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435) 
at android.app.ActivityThread.access$600(ActivityThread.java:168) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5434) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619) 
at dalvik.system.NativeStart.main(Native Method)
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
Wg Sam
  • 61
  • 1
  • 2
  • 11

3 Answers3

1
Bundle extras = getIntent().getExtras();
String carb = extras.getString("Carb");

for more info just check this answer: https://stackoverflow.com/a/5265952/2074990

Community
  • 1
  • 1
bofredo
  • 2,348
  • 6
  • 32
  • 51
  • regarding the logcat-outout you have an invalid integer. Makes my answer obsolete... though it is the nicer approach :) – bofredo Dec 03 '15 at 14:02
  • i face another problem, would you please hlp me take a look. here the link http://stackoverflow.com/questions/34085608/passing-number-from-1st-activity-to-the-number-picker?noredirect=1#comment55925191_34085608 – Wg Sam Dec 04 '15 at 11:30
0

Caused by: java.lang.NumberFormatException: Invalid int: "3809 cal"

You're trying to convert the String "3809 cal" to int/float, while "3809 cal" is not a valid format of int/float.

Check your cal String in the first activity, it must be "3809" instead of "3809 cal".

Rami
  • 7,879
  • 12
  • 36
  • 66
  • but i really dunno where the 3809 cal from i already check there is the code from activity 1 before i pass to the 2nd activity i got create this calculation int finalCalResultValue = calculateFinalCal(tdeeResultValue); finalCal_result.setText(finalCalResultValue + " cal"); – Wg Sam Dec 03 '15 at 14:24
  • With `finalCal_result.setText(finalCalResultValue + " cal");` , you append `"cal"` to your integer so it became an invalid format of integer. – Rami Dec 03 '15 at 14:35
  • Thank @Rami , solve the problem,but now i face another problem, would you please hlp me take a look. here the link http://stackoverflow.com/questions/34085608/passing-number-from-1st-activity-to-the-number-picker?noredirect=1#comment55925191_34085608 – Wg Sam Dec 04 '15 at 11:30
0

There is problem in your string.

LogCat says Invalid int: "3809 cal"

You pass Invalid data in that string.

You can just pass only Integer for convert String to Float

Example : Valid String is "3809".

Instead of "3809 cal".

Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • but i really dunno where the 3809 cal from i already check there is the code from activity 1 before i pass to the 2nd activity i got create this calculation int finalCalResultValue = calculateFinalCal(tdeeResultValue); finalCal_result.setText(finalCalResultValue + " cal"); – Wg Sam Dec 03 '15 at 14:22