Basically, I'm trying to make this eating healthy app based on their BMI. The calculator is done but now I'm stuck at this where the user clicks on one of these 3 buttons and that list in the Spinner
item and the ImageView
changes accordingly (if user clicks breakfast button, then the spinner list and image change to breakfast and so). Even the image is not working. The app runs but when I click the button it terminates and I don't know how to fix it. if it's possible, can you guys also let me know the way I'm doing my Spinner
will work or not?
MainActivity
:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button Toch;
Button BTbtn;
Button LunchBtn;
Button DinnerBtn;
Button Submit;
EditText inweight;
EditText inheight;
EditText inage;
TextView BMR;
RadioButton rdM;
RadioButton rdF;
ImageView maimage;
Spinner spinner;
/**
Button Submit = (Butt
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button Submit = (Button)findViewById(R.id.Sumbit);
final Button Toch = (Button) findViewById(R.id.Toch);
Toch.setOnClickListener(this);
final Button BTbtn = (Button) findViewById(R.id.BTbtn);
BTbtn.setOnClickListener(this);
final Button LunchBtn = (Button) findViewById(R.id.LunchBtn);
LunchBtn.setOnClickListener(this);
final Button DinnerBtn = (Button) findViewById(R.id.DinnerBtn);
DinnerBtn.setOnClickListener(this);
//EditText
final EditText inweight = (EditText) findViewById(R.id.inweight);
final EditText inheight = (EditText) findViewById(R.id.inheight);
final EditText inage = (EditText) findViewById(R.id.inage);
//Text View
final TextView BMR = (TextView) findViewById(R.id.BMR);
// final TextView FDName=(TextView)findViewById(R.id.FDName);
//RadioButton
final RadioButton rdM = (RadioButton) findViewById(R.id.rdM);
final RadioButton rdF = (RadioButton) findViewById(R.id.rdF);
//ImageView
final ImageView maimage = (ImageView) findViewById(R.id.maimage);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.BTbtn: {
maimage.setImageResource(R.drawable.breakfast);
//ArrayAdapter bList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.breakfastList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.LunchBtn: {
maimage.setImageResource(R.drawable.dinner);
//ArrayAdapter lList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.lunchList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.DinnerBtn: {
maimage.setImageResource(R.drawable.lunch);
//ArrayAdapter dList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.dinnerList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.Sumbit: {
double weight = Double.parseDouble(inweight.getText().toString());
double height = Double.parseDouble(inheight.getText().toString());
double age = Double.parseDouble(inage.getText().toString());
double gender;
if (rdM.isChecked()) {
gender = 66;
double ans = gender + (13.7 * weight) + (5 * height) - (6.8 * age);
BMR.setText("" + (int) ans);
}
if (rdF.isChecked()) {
gender = 655;
double ans = gender + (9.6 * weight) + (1.8 * height) - (4.7 * age);
BMR.setText("Your Calories require : " + (int) ans);
}
}
case R.id.Toch: {
inweight.setHint(R.string.chweight);
inheight.setHint(R.string.chhight);
inage.setHint(R.string.chage);
}
}
}
}
R.layout.activity_main
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.cmleu_000.myapplication.MainActivity">
<TextView
android:text="Food Name:"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:id="@+id/BTbtn"
android:text="Breakfast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/LunchBtn"
android:layout_alignRight="@+id/radioGroup2"
android:layout_alignEnd="@+id/radioGroup2" />
<Button
android:id="@+id/LunchBtn"
android:text="Lunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/DinnerBtn"
android:layout_alignRight="@+id/BTbtn"
android:layout_alignEnd="@+id/BTbtn" />
<Button
android:id="@+id/DinnerBtn"
android:text="Dinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/maimage"
android:layout_alignLeft="@+id/LunchBtn"
android:layout_alignStart="@+id/LunchBtn" />
<ImageView
android:id="@+id/maimage"
android:src="@drawable/dinner"
android:layout_width="150dp"
android:layout_height="120dp"
android:layout_above="@+id/qweight"
android:layout_alignLeft="@+id/qhight"
android:layout_alignStart="@+id/qhight" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Sumbit"
android:layout_alignRight="@+id/Sumbit"
android:layout_alignEnd="@+id/Sumbit"
android:id="@+id/radioGroup2">
<RadioButton
android:id="@+id/rdF"
android:text="F"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/rdM"
android:layout_toRightOf="@+id/rdM"
android:layout_toEndOf="@+id/rdM"
android:layout_gravity="left" />
<RadioButton
android:id="@+id/rdM"
android:text="M"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/radioGroup"
android:layout_toRightOf="@+id/qage"
android:layout_toEndOf="@+id/qage"
android:layout_gravity="left" />
</RadioGroup>
<TextView
android:text="Your weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/qweight"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="@+id/inweight"
android:hint="Plase input your weight (KG)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/qhight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Your height"
android:id="@+id/qhight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/inheight"
android:layout_alignRight="@+id/qweight"
android:layout_alignEnd="@+id/qweight" />
<EditText
android:id="@+id/inheight"
android:hint="Please input your height (CM)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/qage"
android:layout_alignRight="@+id/inweight"
android:layout_alignEnd="@+id/inweight" />
<TextView
android:text="Your age"
android:id="@+id/qage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/inage"
android:layout_alignLeft="@+id/BMR"
android:layout_alignStart="@+id/BMR" />
<EditText
android:id="@+id/inage"
android:hint="Please input your age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Sumbit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/Sumbit"
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:text="Your BMR"
android:id="@+id/BMR"
android:textStyle="bold"
android:textSize="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/inheight"
android:layout_alignStart="@+id/inheight" />
<Button
android:text="中文"
android:id="@+id/Toch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/BMR"
android:layout_toLeftOf="@+id/Sumbit"
android:layout_toStartOf="@+id/Sumbit" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_above="@+id/BTbtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Logcat:
08-24 22:43:52.039 3104-3104/com.example.cmleu_000.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cmleu_000.myapplication, PID: 3104
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.cmleu_000.myapplication.MainActivity.onClick(MainActivity.java:92)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-24 22:43:53.601 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 8.662ms
08-24 22:44:10.123 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 5.787ms
*new error after edditing