Java 类必须声明为抽象或实现抽象方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36829998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class must either be declared abstract or implement abstract method?
提问by fatalError
It loos like i'm doing everything right here but I must be missing something. Hoping to get explanation about why I'm getting that error in the first line of my code and what I should do to fix this type of problem? I'm new to android dev.
看起来我在这里做所有事情,但我一定错过了一些东西。希望得到有关为什么我在代码的第一行出现该错误以及我应该如何解决此类问题的解释?我是 android 开发新手。
public class page2 extends AppCompatActivity implements OnClickListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
ImageButton rock, scissors, paper;
Random randomNum = new Random();
int cpuMov;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitypage2); //set what xml page style to display
rock = (ImageButton) findViewById(R.id.imageButtonRock);
scissors = (ImageButton) findViewById(R.id.imageButtonScissors);
paper = (ImageButton) findViewById(R.id.imageButtonPaper);
rock.setOnClickListener(new OnClickListener() { // calling onClick() method
@Override
//method for handling what happens when you click Rock buttonImage.
public void onClick(View v) {
// number between 1-3 inclusive (max-min+1)+1
cpuMov = randomNum.nextInt(3 - 1 + 1) + 1;
buildAlert(cpuMov, "imageButtonRock");
moveViewToScreenCenter(v);//float to center
}
});
scissors.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// number between 1-3 inclusive (max-min+1)+1
cpuMov = randomNum.nextInt(3 - 1 + 1) + 1;
buildAlert(cpuMov, "imageButtonScissors");
moveViewToScreenCenter(v);//float to center
}
});
paper.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// number between 1-3 inclusive (max-min+1)+1
cpuMov = randomNum.nextInt(3 - 1 + 1) + 1;
buildAlert(cpuMov, "imageButtonPaper");
moveViewToScreenCenter(v);//float to center
}
});
} //EoOnCreate
private void moveViewToScreenCenter( View view )
{
RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics( dm );
int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
int originalPos[] = new int[2];
view.getLocationOnScreen( originalPos );
int xDest = dm.widthPixels/2;
xDest -= (view.getMeasuredWidth()/2);
int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
anim.setDuration(5000); // speed of the movement
anim.setFillAfter( true );
view.startAnimation(anim);
}
//builds the message that will be displayed
void appendMessage(AlertDialog.Builder myAlert, String cpuMove, String result) {
myAlert.setMessage("Computer selects " +cpuMove+ ", you " +result+ "!");
myAlert.show();
myAlert.setCancelable(true);
//how to give id to alertDialog to save in strings.xml
}
public void buildAlert(int move, String btnPressed) {
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
String cpuMove;
String result;
if (btnPressed.equals("imageButtonRock")) {
//Toast.makeText(this, "hooray "+move, Toast.LENGTH_SHORT).show();
if (move == 1) {
cpuMove = "Rock";
result = "tied";
//call appendMessage to append vars and display the message
appendMessage(myAlert, cpuMove, result);
}
if (move == 2) {
cpuMove = "Scissors";
result = "won";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
if (move == 3) {
cpuMove = "Paper";
result = "lost";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
} else if (btnPressed.equals("imageButtonScissors")) {
if (move == 1) {
cpuMove = "Rock";
result = "lost";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
if (move == 2) {
cpuMove = "Scissors";
result = "tied";
//call appendMessage to append vars and display the message
appendMessage(myAlert, cpuMove, result);
}
if (move == 3) {
cpuMove = "Paper";
result = "won";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
} else { //imageButton is Scissors
if (move == 1) {
cpuMove = "Rock";
result = "won";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
if (move == 2) {
cpuMove = "Scissors";
result = "lost";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
if (move == 3) {
cpuMove = "Paper";
result = "tied";
//call appendMessage to append vars and display the message
appendMessage(myAlert,cpuMove,result);
}
}
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"page2 Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://rps.rsp/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"page2 Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://rps.rsp/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
}
I know that View.OnClickListener must implement the function onClick(). Am I not doing that?
我知道 View.OnClickListener 必须实现 onClick() 函数。我不这样做吗?
采纳答案by Vucko
You need to implement all the unimplemented methods from the interface you're implementing. In your case the interface is OnClickListener
and it needs to have a method:
您需要从您正在实现的接口中实现所有未实现的方法。在您的情况下,接口是OnClickListener
并且它需要有一个方法:
public abstract void onClick (View v)
public abstract void onClick (View v)
Either remove the interface or implement that method. You can do so easily by placing your cursor on the OnClickListener
and pressing ALT+ENTER
if you're using Android Studioand it'll give you the options on how to fix the error.
删除接口或实现该方法。您可以通过放置在你的光标做很容易OnClickListener
和紧迫ALT+ENTER
如果你使用过Android Studio,它会给你如何修复错误的选项。
As I can see in your code, you've already given your buttons the proper onClickListeners
so the solution might be simply to delete implements OnClickListener
since you're not using it.
正如我在您的代码中看到的那样,您已经为您的按钮提供了正确的选项,onClickListeners
因此解决方案可能只是删除,implements OnClickListener
因为您没有使用它。
回答by Karakuri
Remove implements OnClickListener
from your class definition.
implements OnClickListener
从您的类定义中删除。