Java 未识别findViewById

Java 未识别findViewById,java,android,Java,Android,这是我功能的一部分,android studio没有识别findViewById功能。 谁能帮我找出问题所在吗\ public void onClick(View v) { p1 = (EditText) findViewById(R.id.p1); p2 = (EditText) findViewById(R.id.p2); if (v.getId() == R.id.btnrock) {

这是我功能的一部分,android studio没有识别findViewById功能。 谁能帮我找出问题所在吗\

 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="";
                    }

如果你是碎片

必须使用View v对象来访问findviewById()方法 下面是如何得到它的,还要注意,(EditText)强制转换是多余的

    public void onClick(View v) {
                    p1 =  v.findViewById(R.id.p1);
                    p2 =  v.findViewById(R.id.p2);
    .
    .
    .
    .....
}

此方法是活动还是服务或DTO?另外,请添加类的更多详细信息。该代码是否在
片段中?那就看看吧。