我正在尝试使用radiogroup来确定在初始化无线电组时使用logcat状态崩溃的if语句。
以下是Java类:
public class MainActivity extends Activity {
TextView BMR, TDEE;
EditText feet, inches, weight, age;
RadioGroup sex, activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
feet = (EditText) findViewById(R.id.editFeet);
inches = (EditText) findViewById(R.id.editInches);
weight = (EditText) findViewById(R.id.editWeight);
age = (EditText) findViewById(R.id.editAge);
BMR = (TextView) findViewById(R.id.textBMR);
TDEE = (TextView) findViewById(R.id.textTDEE);
((Button) findViewById(R.id.buttonCalculate)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int sexed = sex.getCheckedRadioButtonId();
int active = activity.getCheckedRadioButtonId();
if (sexed == R.id.radioMale) {
int ft = Integer.parseInt(feet.getText().toString());
int in = Integer.parseInt(inches.getText().toString());
int pounds = Integer.parseInt(weight.getText().toString());
double dAge = Double.parseDouble(age.getText().toString());
int convertedFeet = (ft * 12);
double cm = ((convertedFeet + in) * 2.54);
double kg = (pounds * 0.453592);
double bmr = ((66 + (13.7 * kg)+(5 * cm)- (6.8 * dAge)));
BMR.setText("BMR = " + bmr);
if (active == R.id.radioModerate) {
TDEE.setText("TDEE = " + (bmr * 1.55));
} if (active == R.id.radioVeryActive) {
TDEE.setText("TDEE = " + (bmr * 1.725));
} else {
TDEE.setText("TDEE = " + (bmr * 1.9));
}
} else {
int ft = Integer.parseInt(feet.getText().toString());
int in = Integer.parseInt(inches.getText().toString());
int pounds = Integer.parseInt(weight.getText().toString());
double dAge = Double.parseDouble(age.getText().toString());
int convertedFeet = (ft * 12);
double cm = ((convertedFeet + in) * 2.54);
double kg = (pounds * 0.453592);
double bmr = ((655 + (9.6 * kg)+(1.8 * cm)- (4.7 * dAge)));
BMR.setText("BMR = " + bmr);
if (active == R.id.radioModerate) {
TDEE.setText("TDEE = " + (bmr * 1.55));
} if (active == R.id.radioVeryActive) {
TDEE.setText("TDEE = " + (bmr * 1.725));
} else {
TDEE.setText("TDEE = " + (bmr * 1.9));
}
}
}
});
}
这是我的logcat:
06-12 14:16:39.030: I/Choreographer(859): Skipped 68 frames! The application may be doing too much work on its main thread.
06-12 14:16:39.680: I/Choreographer(859): Skipped 68 frames! The application may be doing too much work on its main thread.
06-12 14:16:39.880: D/gralloc_goldfish(859): Emulator without GPU emulation detected. 06-12 14:16:42.010: I/Choreographer(859): Skipped 44 frames! The application may be doing too much work on its main thread.
06-12 14:16:55.720: W/IInputConnectionWrapper(859): endBatchEdit on inactive InputConnection
06-12 14:17:03.430: D/AndroidRuntime(859): Shutting down VM
06-12 14:17:03.430: W/dalvikvm(859): threadid=1: thread exiting with uncaught exception (group=0xb4a58ba8)
06-12 14:17:03.490: E/AndroidRuntime(859): FATAL EXCEPTION: main
06-12 14:17:03.490: E/AndroidRuntime(859): Process: co.mytms.calorieintakecalculator, PID: 859
06-12 14:17:03.490: E/AndroidRuntime(859): java.lang.NullPointerException
06-12 14:17:03.490: E/AndroidRuntime(859): at co.mytms.calorieintakecalculator.MainActivity$1.onClick(MainActivity.java:37)
06-12 14:17:03.490: E/AndroidRuntime(859): at android.view.View.performClick(View.java:4438)
06-12 14:17:03.490: E/AndroidRuntime(859): at android.view.View$PerformClick.run(View.java:18422)
06-12 14:17:03.490: E/AndroidRuntime(859): at android.os.Handler.handleCallback(Handler.java:733)
06-12 14:17:03.490: E/AndroidRuntime(859): at android.os.Handler.dispatchMessage(Handler.java:95)
06-12 14:17:03.490: E/AndroidRuntime(859): at android.os.Looper.loop(Looper.java:136)
06-12 14:17:03.490: E/AndroidRuntime(859): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-12 14:17:03.490: E/AndroidRuntime(859): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 14:17:03.490: E/AndroidRuntime(859): at java.lang.reflect.Method.invoke(Method.java:515)
06-12 14:17:03.490: E/AndroidRuntime(859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-12 14:17:03.490: E/AndroidRuntime(859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-12 14:17:03.490: E/AndroidRuntime(859): at dalvik.system.NativeStart.main(Native Method)
06-12 14:17:07.810: I/Process(859): Sending signal. PID: 859 SIG: 9