尝试运行此代码时出错。目的是通过edittexts使用户输入权重和时间,并通过微调器选择活动。但是,在将值放入后选择一个选项时出现错误。
以下是代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
countCal = (EditText) view.findViewById(R.id.editText);
weight = (EditText) view.findViewById(R.id.weight);
Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Exercises, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
return view;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
if (countCal.getText().toString().trim().length() > 0 && weight.getText().toString().trim().length() > 0){
if (parent.getItemAtPosition(i).toString().equals("Jogging")) {
Toast.makeText(parent.getContext(), (int) (0.0175 * 7 * Integer.parseInt(weight.getText().toString()) * Integer.parseInt(weight.getText().toString())), Toast.LENGTH_SHORT).show();
} else if (parent.getItemAtPosition(i).toString().equals("Walking")) {
Toast.makeText(parent.getContext(), (int) (0.0175 * 3.5 * Integer.parseInt(weight.getText().toString()) * Integer.parseInt(weight.getText().toString())), Toast.LENGTH_SHORT).show();
} else if (parent.getItemAtPosition(i).toString().equals("Free Weights")) {
Toast.makeText(parent.getContext(), (int) (0.0175 * 4.5 * Integer.parseInt(weight.getText().toString()) * Integer.parseInt(weight.getText().toString())), Toast.LENGTH_SHORT).show();
}
}
}
这是xml
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/weight"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<EditText
android:layout_width="113dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_gravity="left|top"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
这是我的错误日志
08-08 13:23:40.885 2353-2353/stefdude1999.fitness E/AndroidRuntime: FATAL EXCEPTION: main
Process: stefdude1999.fitness, PID: 2353
android.content.res.Resources$NotFoundException: String resource ID #0xc0
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.Toast.makeText(Toast.java:286)
at stefdude1999.fitness.FirstFragment.onItemSelected(FirstFragment.java:75)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:924)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883)
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)
答案 0 :(得分:4)
Toast.makeText的第二个参数是字符串或整数资源ID。您将它传递给计算结果,因此将其视为资源ID。并且该资源ID无效。将整个计算放在Integer.toString()中。
答案 1 :(得分:1)
添加
countCal = (EditText) view.findViewById(R.id.editText);
要 onItemSelected函数