简体   繁体   中英

Code crashing because of If Statements

I get an error when I try to run this code. The purpose is to make the user input weight and time through edittexts, and to choose an activity through a spinner. However, I get an error upon selecting a choice after putting my values in.

Here is the code:

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();
        }

        }
    }

here is the 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" />

Here is my error log

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)

The second parameter to Toast.makeText is a string or integer resource id. You're passing it the result of a computation, so its treating it as a resource id. And that resource id isn't valid. Put the entire calculation inside an Integer.toString().

Just add

countCal = (EditText) view.findViewById(R.id.editText);

To onItemSelected function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM