查找视图按 ID 无法识别



这是我函数的一部分,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(( 方法 这是你如何得到它,还要注意(编辑文本(强制转换是多余的

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

最新更新