0

this is part of my function, android studio isnt recognizing the findViewById function. can anyone please help me figure out the problem is?\

 public void onClick(View v) {
                p1 = (EditText) findViewById(R.id.p1);
                p2 = (EditText) findViewById(R.id.p2);
                if (v.getId() == R.id.btnrock) {
                    if (player1.equals("")) {
                        player1 = "rock";
                        if (p1.getText().toString().length() == 0 || p2.getText().toString().length() == 0) {
                            Toast.makeText(c, "fill both names", Toast.LENGTH_LONG).show();
                        }
                        if (p1.getText().toString().equals(p2.getText().toString())&&p2.getText().toString().length()!=0) {
                            Toast.makeText(c, "use different names", Toast.LENGTH_LONG).show();
                        }
                        if (p2.getText().toString().length()!=0)score.setText("now "+ p2.getText());
                    }
                    else {
                        if (player1.equals("rock"))
                            score.setText("its a tie");
                        if (player1.equals("paper"))
                            score.setText(p1.getText() + " won");
                        if (player1.equals("scissors"))
                            score.setText(p2.getText() + " won");
                        if (player1.equals("lizard"))
                            score.setText(p2.getText() + " won");
                        if (player1.equals("spock"))
                            score.setText(p1.getText() + " won");
                        player1="";
                    }
guipivoto
  • 18,327
  • 9
  • 60
  • 75

1 Answers1

0

If you are in Fragment

You have to use the View v object to access the findviewById() method Here is how you would get it, also note that (EditText) cast is redundant

    public void onClick(View v) {
                    p1 =  v.findViewById(R.id.p1);
                    p2 =  v.findViewById(R.id.p2);
    .
    .
    .
    .....
}
Barungi Stephen
  • 759
  • 8
  • 13