2016-04-24 153 views
1

它像我在这里所做的一切一样,但我必须失去一些东西。希望能够解释为什么我在代码的第一行中遇到错误,以及我应该如何解决这类问题?我是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(); 
} 

}

我知道View.OnClickListener必须实现功能的onClick()。我没有那样做?

回答

3

您需要从您正在实现的接口实现所有未实现的方法。在你的情况下,界面OnClickListener,它需要有一个方法:

public abstract void onClick (View v)

要么删除接口或实现方法。如果您使用的是Android Studio,您可以将光标放在OnClickListener上并按ALT+ENTER,这样可以轻松完成此任务,并为您提供有关如何修复错误的选项。

正如我在你的代码中看到的,你已经给你的按钮适当的onClickListeners,所以解决方案可能只是删除implements OnClickListener,因为你没有使用它。

+1

Ohhhhh好吧,这是有道理的!谢谢。 – fatalError

+0

没有问题...如果这解决了您的问题并为您澄清了问题,请考虑接受解决问题的答案。 – Vucko

+1

alt-enter ftw !! – publicknowledge

2

从您的班级定义中删除implements OnClickListener

相关问题