-2

私はアンドロイドが初めてで、これであなたの助けに本当に感謝しています =)

ユーザーがスピナーを介して選択したさまざまな単語を計算用の数値に変換しようとしています。

私はこれを行う方法がわからず、すでに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

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 に答える