ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • android studio에서 다른 메소드를 호출하는 방법
    카테고리 없음 2020. 8. 15. 13:21

    질문

    안드로이드 스튜디오에서 퀴즈 앱을 만들고 있습니다. 제출할 때 사용자 점수와 함께 토스트를 표시해야합니다. submitOnClick 메서드 내부에서 내 calculateScore 메서드를 호출하고 싶지만 calculateScore 메서드로 인해 앱이 중단됩니다.

    제출 내에서 calculateScore를 호출하면 앱이 충돌하고 onClick이라고 호출하면 앱이 충돌합니다.

    내 코드 :

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final Button button = findViewById(R.id.submit);
            button.setOnClickListener(submitOnClick);
    
        }
    
        final View.OnClickListener submitOnClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateScore();
    
                Toast.makeText(MainActivity.this, "Your score is " + score, Toast.LENGTH_LONG).show();
            }
        };
    
            public void checkQuestion1(View view) {
                boolean checked = ((RadioButton) view).isChecked();
    
                // Check which radio button was clicked
                switch (view.getId()) {
                    case R.id.right:
                        if (checked)
                            score += 1;
                        break;
                    case R.id.wrong:
                        if (checked)
                            score += 0;
                        break;
                }
    
            }
    
            public void checkQuestion2(View view) {
    
                CheckBox function = findViewById(R.id.checkbox_function);
                CheckBox undefined = findViewById(R.id.checkbox_undefined);
                CheckBox booleans = findViewById(R.id.checkbox_booleans);
    
                boolean isCheckedFunction = function.isChecked();
                boolean isCheckedUndefined = undefined.isChecked();
                boolean isCheckedBooleans = booleans.isChecked();
    
                if (isCheckedUndefined && isCheckedBooleans && !isCheckedFunction) {
                    score += 1;
                } else {
                    score += 0;
                }
    
            }
    
            public void checkQuestion3(View view) {
    
                EditText userInput = findViewById(R.id.inputAnswer);
                String name = userInput.getText().toString();
    
                if (name.trim().equalsIgnoreCase("Class")) {
                    score += 1;
                } else {
                    score += 0;
                }
            }
    
            public void checkQuestion4(View view) {
                boolean checked = ((RadioButton) view).isChecked();
                switch (view.getId()) {
                    case R.id.correct:
                        if (checked)
                            score += 1;
                        break;
                    case R.id.incorrect:
                        if (checked)
                            score += 0;
                        break;
                }
    
            }
    
            public void checkQuestion5(View view) {
    
                CheckBox correctOne = findViewById(R.id.checkbox_correct_one);
                CheckBox correctTwo = findViewById(R.id.checkbox_correct_two);
                CheckBox incorrectOne = findViewById(R.id.checkbox_incorrect_one);
                CheckBox incorrectTwo = findViewById(R.id.checkbox_incorrect_two);
                boolean isCheckedCorrectOne = correctOne.isChecked();
                boolean isCheckedCorrectTwo = correctTwo.isChecked();
                boolean isCheckedIncorrectOne = incorrectOne.isChecked();
                boolean isCheckedIncorrectTwo = incorrectTwo.isChecked();
    
                if (isCheckedCorrectOne && isCheckedCorrectTwo && !isCheckedIncorrectOne & !isCheckedIncorrectTwo) {
                    score += 1;
                } else {
                    score += 0 ;
                }
    
            }
    
    
            public void calculateScore(){
                checkQuestion2(null);
                checkQuestion1(null );
                checkQuestion3(null);
                checkQuestion4(null);
                checkQuestion5(null);
            }

    logcat : --------- 충돌 시작

    2019-11-04 10 : 09 : 57.514 11886-11886 / com.example.android.quizzapp E / AndroidRuntime : FATAL EXCEPTION : main 프로세스 : com.example.android.quizzapp, PID : 11886 java.lang.NullPointerException : null 객체에서 가상 메소드 'boolean android.widget.CompoundButton.isChecked ()'호출 시도 참고 com.example.android.quizzapp.MainActivity.checkQuestion1 (MainActivity.java:39) com.example.android.quizzapp.MainActivity.calculateScore (MainActivity.java:122) com.example.android.quizzapp.MainActivity $ 1.onClick (MainActivity.java:32)에서 android.view.View.performClick (View.java:5610)에서 android.view.View $ PerformClick.run (View.java:22265)에서 android.os.Handler.handleCallback (Handler.java:751)에서 android.os.Handler.dispatchMessage (Handler.java:95)에서 android.os.Looper.loop (Looper.java:154)에서 android.app.ActivityThread.main (ActivityThread.java:6077)에서 java.lang.reflect.Method.invoke (Native Method)에서 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:866) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:756)


    답변1

    당신은 전화

    checkQuestion1(null )

    null로 인해 NPE와 충돌합니다.



     

     

     

     

    출처 : https://stackoverflow.com/questions/58689315/how-to-call-many-methods-in-another-in-android-studio

    댓글

Designed by Tistory.