[英]Method can't be found once it's respective ImageButton is pressed
I'm trying to create a simple rock, paper, scissors app but when the user selects the ImageButtons
related to r,p, or s I get a 我正在尝试创建一个简单的石头,纸张,剪刀应用程序,但是当用户选择与r,p或s相关的ImageButtons
,我会得到一个
"Could not find method in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id"
When I comment out those methods in the java file I get an error in the xml
saying that it 当我注释掉Java文件中的那些方法时,我在xml
得到一个错误,说它
can't find the methods being called with the onClick attribute
and the errors go away once I remove the "//" So androidstudio seems to see the methods but for some reason it can't find them once they're there and being called? 并且一旦我删除了“ //”,错误就消失了,所以androidstudio似乎看到了这些方法,但是由于某种原因,一旦它们出现并被调用,便找不到它们了? If it matters, this is the second activity in the app.(The other one being the main) This is a common error from what I've seen but I was unable to find a solution that worked for my situation. 如果重要的话,这是应用程序中的第二个活动。(另一个是主要活动)这是我所看到的常见错误,但是我找不到适合我情况的解决方案。
This is my xml code(activity_rps.xml), I removed everything but the ImageButtons that are causing the problem 这是我的xml代码(activity_rps.xml),我删除了导致问题的ImageButtons之外的所有内容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.gamegroup.rps">
<ImageButton
android:id="@+id/rps_paper"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/rps_paper"
android:onClick="userChoicePaper" />
<ImageButton
android:id="@+id/rps_scissors"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/rps_scissors"
android:onClick="userChoiceScissors" />
<ImageButton
android:id="@+id/rps_rock"
android:layout_width="128dp"
android:layout_height="128dp"
android:background="@drawable/rps_rock"
android:onClick="userChoiceRock"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This is my java code(rps.java). 这是我的Java代码(rps.java)。 I've removed a lot that wasn't related to the problem and the sections that are commented out are from me trying to narrow down the issue. 我删除了很多与问题无关的内容,而被注释掉的部分是我试图缩小问题范围的。
package com.example.android.gamegroup;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;
public class rps extends AppCompatActivity {
//int userChoice, pcChoice, winner, userWinCount, pcWinCount;
//ImageButton choiceRock = (ImageButton) findViewById(R.id.rps_rock);
//ImageButton choicePaper = (ImageButton) findViewById(R.id.rps_paper);
//ImageButton choiceScissors = (ImageButton)
findViewById(R.id.rps_scissors);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rps);
//userWinCount = 0;
//pcWinCount = 0;
}
// Generate a random integer ranging from 1 to 3 for
public void setPcChoice() {
int randNum;
Random randInt = new Random();
randNum = randInt.nextInt(3) + 1;
//pcChoice = randNum;
}
// Change userChoice to 1 for picking rock, assign pcChoice, assign
public void userChoiceRock(View view)
{
//choiceRock.setImageResource(R.drawable.rps_blue_rock);
//userChoice = 1;
setPcChoice();
//setWinner();
}
// Change userChoice to 2 for picking paper, assign pcChoice, assign winner
public void userChoicePaper(View view)
{
//choicePaper.setImageResource(R.drawable.rps_blue_paper);
//userChoice = 2;
setPcChoice();
//setWinner();
}
// Change userChoice to 3 for picking scissors, assign pcChoice, assign winner******************
public void userChoiceScissors(View view)
{
//choiceScissors.setImageResource(R.drawable.rps_blue_scissors);
//userChoice = 3;
setPcChoice();
//setWinner();
}
}
It is better to use OnClickListener
in your java code in stead of using it in your xml
. 最好在Java代码中使用OnClickListener
而不是在xml
中使用它。 To do that you have to modify your rps.java
as below: 为此,您必须按如下所示修改rps.java
:
public class rps extends AppCompatActivity {
//int userChoice, pcChoice, winner, userWinCount, pcWinCount;
ImageButton choiceRock;
ImageButton choicePaper;
ImageButton choiceScissors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rps);
choiceRock = (ImageButton) findViewById(R.id.rps_rock);
choicePaper = (ImageButton) findViewById(R.id.rps_paper);
choiceScissors = (ImageButton) findViewById(R.id.rps_scissors);
choiceRock.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//choiceRock.setImageResource(R.drawable.rps_blue_rock);
//userChoice = 1;
setPcChoice();
//setWinner();
}
});
choicePaper.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//choicePaper.setImageResource(R.drawable.rps_blue_paper);
//userChoice = 2;
setPcChoice();
//setWinner();
}
});
choiceScissors.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//choiceScissors.setImageResource(R.drawable.rps_blue_scissors);
//userChoice = 3;
setPcChoice();
//setWinner();
}
});
//userWinCount = 0;
//pcWinCount = 0;
}
public void setPcChoice() {
int randNum;
Random randInt = new Random();
randNum = randInt.nextInt(3) + 1;
//pcChoice = randNum;
}
}
And finally, remove the android:onClick
parts from the 3 ImageButton
in your xml
file. 最后,从xml
文件中的3 ImageButton
中删除android:onClick
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.