这是我的第一个活动,我想将值传递给第二个活动进行计算并在textview中显示结果。但是我不知道为什么我在活动1中键入所有详细信息并单击自定义按钮时会跳出来从应用程序
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;
}
这是我的第二项活动,是要从活动1获取评估者并进行计算并在textview cp中显示结果。但是我不知道为什么我在活动1中键入所有详细信息并单击自定义按钮时会从应用程序中跳出
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);
}
这是日志猫错误
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)
最佳答案
Bundle extras = getIntent().getExtras();
String carb = extras.getString("Carb");
有关更多信息,请检查以下答案:
https://stackoverflow.com/a/5265952/2074990