我正在努力创建一个基本的连接3应用程序作为课程挑战的一部分。
我遇到的问题是我无法访问类Players
中的方法,以便设置哪些按钮被哪些玩家按下:Cannot resolve method 'noughtsPlayer'
。
我在Players
方法上创建了onCreate
的实例,并将其声明在脚本的顶部。该实例本身已被识别,但由于某些原因,所包含的方法noughtsPlayer
和crossesPlayer
不是。
MainActivity.java
package com.example.richardcurteis.connect3;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public boolean noughtsTurn;
ArrayList board;
String players;
public void receiveClick(View view) {
String buttonPressed = (String) view.getTag();
board.remove(buttonPressed);
if (view instanceof Button) {
Button B = (Button) view;
B.setText(noughtsTurn ? players.noughtsPlayer() : players.crossesPlayer());
}
}
class Players {
public String noughtsPlayer() { return "O"; }
public String crossesPlayer() { return "X"; }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
boolean noughtsTurn = true;
board = new ArrayList();
for (int x = 0; x < getBoardSize(); x++) {
String y = String.valueOf(x);
board.add(y);
}
Players players = new Players();
}
public int getBoardSize() {
int buttonCount = 0;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
if (view instanceof Button) {
buttonCount++;
}
}
}
}
return buttonCount;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
您拥有全局变量String players
和本地变量Players players
。
这是个问题。
并且类String没有这些方法。
要解决此问题,请将String players;
更改为Players players;
和Players players = new Players();
到players = new Players();