一旦用户从下拉框中选择食物名称,我试图让我的程序正确显示卡路里的总量。它将显示每个食物名称右侧的卡路里量。但是,单击总按钮后,在底部,我希望它在文本视图区域中显示金额。现在,当我点击总按钮时,整个应用程序崩溃(没有给出错误消息)。任何有关这方面的帮助将不胜感激。我觉得有更好的方法可以做我想做的事情,但我实施的方法是我知道的唯一方法。似乎无法在网上找到任何其他内容。我的代码:
public class CalorieTracker extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
TextView sp1;
TextView sp2;
TextView sp3;
TextView sp4;
TextView sp5;
Spinner spinner1;
Spinner spinner2;
Spinner spinner3;
Spinner spinner4;
Spinner spinner5;
int total;
TextView caloriesNum;
List<String> foods = new ArrayList<String>();
List<String> calories = new ArrayList<String>();
int Cal1;
int Cal2;
int Cal3;
int Cal4;
int Cal5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calorie_tracker);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
spinner5 = (Spinner) findViewById(R.id.spinner5);
sp1 = (TextView) findViewById(R.id.spinner1TextView);
sp2 = (TextView) findViewById(R.id.spinner2TextView);
sp3 = (TextView) findViewById(R.id.spinner3TextView);
sp4 = (TextView) findViewById(R.id.spinner4TextView);
sp5 = (TextView) findViewById(R.id.spinner5TextView);
caloriesNum = (TextView) findViewById(R.id.caloriesNumbers);
populateSpinner();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.foods_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
spinner3.setOnItemSelectedListener(this);
spinner4.setOnItemSelectedListener(this);
spinner5.setOnItemSelectedListener(this);
}
public void populateSpinner() {
foods.add("");
foods.add("Chicken");
foods.add("Pizza");
foods.add("Eggs");
foods.add("Spaghetti");
foods.add("Salad");
foods.add("Ice Cream");
foods.add("Bacon");
foods.add("Banana");
foods.add("Bagel");
foods.add("Beef Roast");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, foods);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);
spinner3.setAdapter(adapter);
spinner4.setAdapter(adapter);
spinner5.setAdapter(adapter);
calories.add("0");
calories.add("300");
calories.add("365");
calories.add("70");
calories.add("325");
calories.add("105");
calories.add("340");
calories.add("90");
calories.add("60");
calories.add("75");
calories.add("315");
}
public void addCalories(View v) {
Cal1 = Integer.parseInt(sp1.getText().toString());
Cal2 = Integer.parseInt(sp2.getText().toString());
Cal3 = Integer.parseInt(sp3.getText().toString());
Cal4 = Integer.parseInt(sp4.getText().toString());
Cal5 = Integer.parseInt(sp5.getText().toString());
total = Cal1 + Cal2 + Cal3 + Cal4 + Cal5;
caloriesNum.setText(total);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_calorie_tracker, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.spinner1:
String strCal1 = calories.get(position).toString();
sp1.setText(strCal1);
break;
case R.id.spinner2:
String strCal2 = calories.get(position).toString();
sp2.setText(strCal2);
break;
case R.id.spinner3:
String strCal3 = calories.get(position).toString();
sp3.setText(strCal3);
break;
case R.id.spinner4:
String strCal4 = calories.get(position).toString();
sp4.setText(strCal4);
break;
case R.id.spinner5:
String strCal5 = calories.get(position).toString();
sp5.setText(strCal5);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_below="@+id/spinner1"
android:layout_marginTop="25dp"
android:layout_alignParentLeft="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner3"
android:layout_below="@+id/spinner2"
android:layout_marginTop="25dp"
android:layout_alignParentLeft="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner4"
android:layout_below="@+id/spinner3"
android:layout_marginTop="25dp"
android:layout_alignParentLeft="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner5"
android:layout_below="@+id/spinner4"
android:layout_alignParentLeft="true"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/saveButton"
android:layout_alignTop="@+id/backButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginRight="10dp"
android:background="#ff00cc27" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/backButton"
android:layout_marginRight="10dp"
android:layout_marginBottom="32dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/saveButton"
android:layout_toEndOf="@+id/saveButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Total Calories:"
android:id="@+id/totalCaloriesTextView"
android:layout_marginBottom="42dp"
android:layout_above="@+id/saveButton"
android:layout_alignLeft="@+id/saveButton"
android:layout_alignStart="@+id/saveButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:id="@+id/caloriesNumbers"
android:layout_alignTop="@+id/totalCaloriesTextView"
android:layout_toRightOf="@+id/totalCaloriesTextView"
android:layout_toEndOf="@+id/totalCaloriesTextView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:layout_marginRight="10dp"
android:onClick="addCalories"
android:id="@+id/totalButton"
android:layout_alignTop="@+id/backButton"
android:layout_toRightOf="@+id/backButton"
android:layout_toEndOf="@+id/backButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Calories"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:id="@+id/spinner1TextView"
android:layout_alignRight="@+id/totalButton"
android:layout_alignEnd="@+id/totalButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Calories"
android:layout_marginTop="25dp"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:id="@+id/spinner2TextView"
android:layout_below="@+id/spinner1TextView"
android:layout_alignRight="@+id/spinner1TextView"
android:layout_alignEnd="@+id/spinner1TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Calories"
android:id="@+id/spinner3TextView"
android:layout_marginTop="25dp"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/spinner2TextView"
android:layout_alignRight="@+id/spinner2TextView"
android:layout_alignEnd="@+id/spinner2TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Calories"
android:layout_marginTop="25dp"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:id="@+id/spinner4TextView"
android:layout_below="@+id/spinner3TextView"
android:layout_alignRight="@+id/spinner3TextView"
android:layout_alignEnd="@+id/spinner3TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Calories"
android:layout_marginTop="25dp"
android:layout_marginRight="30dp"
android:id="@+id/spinner5TextView"
android:layout_below="@+id/spinner4TextView"
android:layout_alignParentRight="true"
android:layout_alignRight="@+id/spinner4TextView"
android:layout_alignEnd="@+id/spinner4TextView" />
logcat的:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x384
at android.content.res.Resources.getText(Resources.java:274)
at android.widget.TextView.setText(TextView.java:4122)
at buckystutorial.foodsspinneritems.CalorieTracker.addCalories(CalorieTracker.java:127)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
从
更改方法的签名public void addCalories()
到
public void addCalories(View v)
此外,理解为什么你无法读取logcat是非常重要的 - 如果没有它,你将无法正常进行。