简体   繁体   中英

Android Java calling method

So, I tried to call all the methods from overView but is not working, I tried with "view" in the parentheses and then is constantly crashing when I start the app.... Can someone help, this is the whole code. I tried not to include the first four methods but then is not working the way it's suppose to.

public class MainActivity extends AppCompatActivity {

int health = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void checkQuestionOne(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
        case R.id.question_one_yes:
            if(checked)
                health += 1;
            break;
        case R.id.question_one_no:
            if(checked)
                health -= 1;
            break;
    }

}

public void checkQuestionTwo(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
        case R.id.question_two_yes:
            if(checked)
                health += 1;
            break;
        case R.id.question_two_no:
            if(checked)
                health -= 1;
            break;
    }

}

public void checkQuestionThree(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
        case R.id.question_three_yes:
            if(checked)
                health += 1;
            break;
        case R.id.question_three_no:
            if(checked)
                health -= 1;
            break;
    }

}

public void checkQuestionFour(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
        case R.id.question_four_yes:
            if(checked)
                health += 1;
            break;
        case R.id.question_four_no:
            if(checked)
                health -= 1;
            break;
    }

}

public void checkQuestionFive() {
    EditText gettingQuestionFive = findViewById(R.id.sleep_hours);
    int answerQuestionFive = Integer.parseInt(gettingQuestionFive.getText().toString());

    if (answerQuestionFive > 7) {
        health += 1;
    } else if (answerQuestionFive == 7) {
        health += 1;
    } else {
        health -=1;
    }
}

private void displayMessage(String message) {
    TextView orderSummaryTextView = (TextView) findViewById(R.id.summary);
    orderSummaryTextView.setText(message);
}

public void overView(View view){
    checkQuestionOne();
    checkQuestionTwo();
    checkQuestionThree();
    checkQuestionFour();
    checkQuestionFive();
    String Message = health + " is your score. If is 3 or above, you have a healthy life";
    displayMessage(Message);

}
}

I think you have forgot to call the overView() method in the onCreate() method of activity. You can call it like this.

 @Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     //Pass your view in argument
     overView(View view)
}

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