0

Problem

I have 2 spinners in my app which are supposed to change textviews, and then I press a button to calculate the result and display on screen. Currently the spinners display correctly, but when the button is pressed the app crashes.

Previously I had 2 edit text where users could type their numbers and it all worked perfectly, but now it crashes with spinner, but I really do need spinners in the app for ease of use so I must figure this out. My spinners have 120 items so I'll just post a short snippet of code. Any help appreciated! I have been struggling with this for yonks now.

Spinner code

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String spin = spinner.getSelectedItem().toString();
            switch (spin) {
                case "1":

                    numspace1.setText("0");

                    break;
                case "2":

                    numspace1.setText("83");

                    break;
                case "3":

                    numspace1.setText("174");

                    break;
                case "4":

                    numspace1.setText("276");

                    break;
                case "5":

                    numspace1.setText("388");

Button code

normal.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            InputMethodManager inputManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);

            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            Double num1 = Double.parseDouble(numspace1.getText().toString());
            Double num2 = Double.parseDouble(numspace2.getText().toString());
            Double vari1 = Double.parseDouble(var1.getText().toString());
            Double vari2 = Double.parseDouble(var2.getText().toString());
            Double vari3 = Double.parseDouble(var3.getText().toString());
            Double vari4 = Double.parseDouble(var4.getText().toString());
            Double vari5 = Double.parseDouble(var5.getText().toString());
            Double vari6 = Double.parseDouble(var6.getText().toString());
            Double vari7 = Double.parseDouble(var7.getText().toString());
            Double vari8 = Double.parseDouble(var8.getText().toString());
            Double vari9 = Double.parseDouble(var9.getText().toString());
            Double vari10 = Double.parseDouble(var10.getText().toString());
            Double vari11 = Double.parseDouble(var11.getText().toString());
            Double vari12 = Double.parseDouble(var12.getText().toString());
            Double vari13 = Double.parseDouble(var13.getText().toString());
            Double vari14 = Double.parseDouble(var14.getText().toString());
            Double vari15 = Double.parseDouble(var15.getText().toString());
            //do the calculation
            Double a1 = (num2 - num1) / vari1;
            Double a2 = (num2 - num1) / vari2;
            Double a3 = (num2 - num1) / vari3;
            Double a4 = (num2 - num1) / vari4;
            Double a5 = (num2 - num1) / vari5;
            Double a6 = (num2 - num1) / vari6;
            Double a7 = (num2 - num1) / vari7;
            Double a8 = (num2 - num1) / vari8;
            Double a9 = (num2 - num1) / vari9;
            Double a10 = (num2 - num1) / vari10;
            Double a11 = (num2 - num1) / vari11;
            Double a12 = (num2 - num1) / vari12;
            Double a13 = (num2 - num1) / vari13;
            Double a14 = (num2 - num1) / vari14;
            Double a15 = (num2 - num1) / vari15;
            //set the value to the textview, to display on screen.
            l1.setText(String.format("%.0f", a1));
            l2.setText(String.format("%.0f", a2));
            l3.setText(String.format("%.0f", a3));
            l4.setText(String.format("%.0f", a4));
            l5.setText(String.format("%.0f", a5));
            l6.setText(String.format("%.0f", a6));
            l7.setText(String.format("%.0f", a7));
            l8.setText(String.format("%.0f", a8));
            l9.setText(String.format("%.0f", a9));
            l10.setText(String.format("%.0f", a10));
            l11.setText(String.format("%.0f", a11));
            l12.setText(String.format("%.0f", a12));
            l13.setText(String.format("%.0f", a13));
            l14.setText(String.format("%.0f", a14));
            l15.setText(String.format("%.0f", a15));
        }
    });

Crash Log

09-02 10:46:48.444 5337-5337/com.elyrs.pocketcalcs E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.elyrs.pocketcalcs, PID: 5337
                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.view.View.getWindowToken()' on a null object reference
                                                                         at com.elyrs.pocketcalcs.AgilCalc$4.onClick(AgilCalc.java:1373)
                                                                         at android.view.View.performClick(View.java:5637)
                                                                         at android.view.View$PerformClick.run(View.java:22429)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
09-02 10:46:48.450 1750-3633/system_process W/ActivityManager:   Force finishing activity com.elyrs.pocketcalcs/.AgilCalc
09-02 10:46:48.650 1750-4649/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
09-02 10:46:48.650 1750-4649/system_process D/OpenGLRenderer: Swap behavior 1
09-02 10:46:48.651 1750-4649/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-02 10:46:48.651 1750-4649/system_process D/OpenGLRenderer: Swap behavior 0
09-02 10:46:48.656 1279-1282/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-02 10:46:48.696 1279-1282/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-02 10:46:48.706 1279-1282/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-02 10:46:49.018 1750-1796/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{f371862 u0 com.elyrs.pocketcalcs/.AgilCalc t5 f}
09-02 10:46:49.104 5337-5344/com.elyrs.pocketcalcs W/art: Suspending all threads took: 70.046ms
09-02 10:46:49.319 1750-1760/system_process I/art: Background partial concurrent mark sweep GC freed 29056(1626KB) AllocSpace objects, 10(200KB) LOS objects, 25% free, 11MB/15MB, paused 9.897ms total 856.623ms
09-02 10:46:54.052 5337-5344/com.elyrs.pocketcalcs W/art: Suspending all threads took: 35.882ms
09-02 10:46:54.757 3455-6219/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
09-02 10:46:58.518 1750-1796/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
MadeinTYO
  • 9
  • 3

0 Answers0