-2

我是 android 新手,非常感谢您在这方面的帮助 =)

我正在尝试将用户通过微调器选择的各种单词转换为用于计算的数字。

我不知道该怎么做,而且已经做了 2 天了。

下面是代码。(我知道这是错误的,因为我使用了另一个 BMI 指南来帮助构建下面的代码,但是,由于用户输入的数据首先是数字,所以它与我的情况不同。)

再次非常感谢大家的帮助,真的很感激。

package com.example.mygpa;

public class Main extends Activity {

    // declare the references for the UI elements
    Spinner courseSpinner;
    Spinner semesterSpinner;
    EditText currentGPA;

    /** Called when the activity is first created. */
    @SuppressLint ("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Spinner Course_Add_Spinner = Create_Course_Add_Spinner();
            Spinner Semester_Add_Spinner = Create_Semester_Add_Spinner(null);

            // load the references to the widgets 
            courseSpinner = (Spinner)findViewById(R.id.spinner_course);
            semesterSpinner = (Spinner)findViewById(R.id.spinner_semester);
            currentGPA = (EditText)findViewById(R.id.current_gpa);

        }catch (Exception e) {
            Log.e("ERROR", e.toString());
            e.printStackTrace();
        }
        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

     // handler that we defined in "onClick" attribute of the button
        // get called when the button is clicked
        public void calculateClickHandler(View view) {
            // make sure we handle the click of the calculator button
            if (view.getId() == R.id.btncalculate) {

                int course = getSelectedCourse();
                int semester = getSelectedSemester();
                int gpavalue = Integer.parseInt(currentGPA.getText().toString());

                int gpaValue = (((gpavalue / course) + (semester*4)) / (course+semester));

                Intent intent = new Intent (Main.this, Main2.class);
                intent.putExtra("GpaValue", gpaValue);
                startActivity(intent);
                }
            }

        public int getSelectedCourse() {

            try
            {
            String Text = courseSpinner.getSelectedItem().toString();
            String Text2 = semesterSpinner.getSelectedItem().toString();
            int value = Integer.parseInt(Text);
            int value2 = Integer.parseInt(Text2);
            }
            catch(NumberFormatException e)
            {
                e.printStackTrace();
            }


            if (courseSpinner.equals("Hospitality and Tourism Management") && semesterSpinner.equals("Year 1 Semester 1")) {
                return (int) (0);
            } else if  (courseSpinner.equals("Hospitality and Tourism Management") && semesterSpinner.equals("Year 1 Semester 2")) {
                return (int) (22);
            } else if  (courseSpinner.equals("Hospitality and Tourism Management") && semesterSpinner.equals("Year 2 Semester 1")) {
                return (int) (44);
            } else if  (courseSpinner.equals("Hospitality and Tourism Management") && semesterSpinner.equals("Year 2 Semester 2")) {
                return (int) (62);
            } else if  (courseSpinner.equals("Hospitality and Tourism Management") && semesterSpinner.equals("Year 3 Semester 1")) {
                return (int) (77);
            } else {
                return (int) (101);
            }
        }

        public int getSelectedSemester() {

            try
            {

                String Text = semesterSpinner.getSelectedItem().toString();
                int value = Integer.parseInt(Text);
            }
            catch(NumberFormatException e)
            {
                e.printStackTrace();
            }

                if (semesterSpinner.equals("Year 1 Semester 1")) {
                    return (int) (22);
                } else if  (semesterSpinner.equals("Year 1 Semester 2")) {
                    return (int) (22);
                } else if  (semesterSpinner.equals("Year 2 Semester 1")) {
                    return (int) (18);
                } else if  (semesterSpinner.equals("Year 2 Semester 2")) {
                    return (int) (15);
                } else if  (semesterSpinner.equals("Year 3 Semester 1")) {
                    return (int) (24);
                } else {
                    return (int) (18);
                }   
        }

    // Creates the Course Spinner for the Add section sends the choice made to
    // a method for populating the Semester spinner
    public Spinner Create_Course_Add_Spinner() {
        Spinner Course_Add_Spinner = (Spinner) findViewById(R.id.spinner_course);
        String[] Course_Add_Spinner_Array = getResources().getStringArray(R.array.course_array);
        SpinnerAdapter Course_Add_Spinner_Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, Course_Add_Spinner_Array);
        Course_Add_Spinner.setAdapter(Course_Add_Spinner_Adapter);
        Course_Add_Spinner.setOnItemSelectedListener(new Course_Add_Spinner_Listener());
        return Course_Add_Spinner;
    }

    // The listener for the Course Spinner that sends whatever choice made to
    // the method that populates the second spinner.
    public class Course_Add_Spinner_Listener implements
            OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> Course_Add_Adapter_View, View v, int position, long row) {
            String Course_Add_Choice = Course_Add_Adapter_View.getItemAtPosition(position).toString();
            Create_Semester_Add_Spinner(Course_Add_Choice);
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    }

    // Create the Semester Spinner for the Add section
    public Spinner Create_Semester_Add_Spinner(String course_array) {
        String[] Semester_Add_Spinner_Array = null;
        Spinner Semester_Add_Spinner = (Spinner) findViewById(R.id.spinner_semester);

        if (course_array.equals("Hospitality and Tourism Management")) {
            Semester_Add_Spinner_Array = getResources().getStringArray(R.array.HTM);

        } else if (course_array.equals("Sports and Wellness")) {
            Semester_Add_Spinner_Array = getResources().getStringArray(R.array.SW);

        } else Semester_Add_Spinner_Array = getResources().getStringArray(R.array.HTM);

        SpinnerAdapter Semester_Add_Spinner_Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, Semester_Add_Spinner_Array);
        Semester_Add_Spinner.setAdapter(Semester_Add_Spinner_Adapter);
        return Semester_Add_Spinner;
        }
}

根据要求,日志猫报告错误发生的位置。

04-20 23:30:13.759: W/dalvikvm(26872): threadid=1: thread exiting with uncaught exception (group=0x40a96930)
04-20 23:30:13.759: E/AndroidRuntime(26872): FATAL EXCEPTION: main
04-20 23:30:13.759: E/AndroidRuntime(26872): java.lang.IllegalStateException: Could not execute method of the activity
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.view.View$1.onClick(View.java:3599)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.view.View.performClick(View.java:4204)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.view.View$PerformClick.run(View.java:17360)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.os.Handler.handleCallback(Handler.java:725)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.os.Looper.loop(Looper.java:137)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.app.ActivityThread.main(ActivityThread.java:5237)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at java.lang.reflect.Method.invoke(Method.java:511)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at dalvik.system.NativeStart.main(Native Method)
04-20 23:30:13.759: E/AndroidRuntime(26872): Caused by: java.lang.reflect.InvocationTargetException
04-20 23:30:13.759: E/AndroidRuntime(26872):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at java.lang.reflect.Method.invoke(Method.java:511)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at android.view.View$1.onClick(View.java:3594)
04-20 23:30:13.759: E/AndroidRuntime(26872):    ... 11 more
04-20 23:30:13.759: E/AndroidRuntime(26872): Caused by: java.lang.NullPointerException
04-20 23:30:13.759: E/AndroidRuntime(26872):    at com.example.mygpa.Main.getSelectedCourse(Main.java:73)
04-20 23:30:13.759: E/AndroidRuntime(26872):    at com.example.mygpa.Main.calculateClickHandler(Main.java:57)
04-20 23:30:13.759: E/AndroidRuntime(26872):    ... 14 more
4

2 回答 2

2
myInteger = Integer.parseInt(myString);
于 2013-04-17T20:06:15.420 回答
0

您可以从微调器获取值并使用如下值。

try
{
     Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
     String Text = mySpinner.getSelectedItem().toString();
     int value= Integer.parseInt(Text);
}
catch(NumberFormatException e)
{
    e.printStackTrace();
}
于 2013-04-18T17:28:09.340 回答