Setter / Getter方法不起作用

时间:2017-02-19 17:31:32

标签: java android

您好我在我的主要活动中访问视图类中的变量/方法时遇到问题。我只需要从GameView.java返回turns int,并能够在MainActivity中访问它,然后将其插入到我的数据库中。我尝试使用GameView mGameView = new GameView(getApplicationContext());但这不起作用,我无法使用此方法从GameView访问任何方法。

我的GameView课程:

public class GameView extends View {


    int mcolumns = 5;
    int mrows = 5;
    private NetwalkGrid mGame = new NetwalkGrid(mcolumns, mrows);
    private GestureDetector mGestureDetector;
    Random rand = new Random();
    int sizeSqX;
    int sizeSqY;
    int sizeSq;

    int turns = 0;



    Paint bgPaint;


    public GameView(Context context) {
        super(context);
        init();
    }

    private void init() {



    bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(0xff0000ff);
    mGame.gridCopy();

        for (int col = 0; col < mGame.getColumns(); col++) {
            for (int row = 0; row < mGame.getRows(); row++) {

                int num = rand.nextInt(3) + 1;

                for (int turns = 1; turns < num; turns++) {
                    mGame.rotateRight(col, row);
                }
            }
        }



    }


    @Override
    protected void onDraw(Canvas canvas) {



        canvas.drawColor(Color.BLACK);

        sizeSqX = getWidth() / mcolumns;
        sizeSqY = getHeight() / mrows;

        if (sizeSqX < sizeSqY) {
            sizeSq = sizeSqX;
        }
        else {

            sizeSq = sizeSqY;
        }

        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder);
        int cellContent;

        int square = 140;
        for (int col = 0; col < mGame.getColumns(); col++) {
            for (int row = 0; row < mGame.getRows(); row++) {

                cellContent = mGame.getGridElem(col,row);
                if (cellContent == 1) {
                    b = BitmapFactory.decodeResource(getResources(), R.drawable.up_down); // Image for down position
                    if (cellContent == 65 && mGame.checkWin()) {
                        b = BitmapFactory.decodeResource(getResources(), R.drawable.up_down_connected);
                    }
                }
                else if (cellContent == 2) {
                    b = BitmapFactory.decodeResource(getResources(), R.drawable.left_right); // Right position
                    if (mGame.checkWin()) {
                        b = BitmapFactory.decodeResource(getResources(), R.drawable.left_right_connected);
                    }
                }
                else if (cellContent == 3) {
                    b = BitmapFactory.decodeResource(getResources(), R.drawable.right_down); // Down right position WORKS
                    if (mGame.checkWin()) {
                        b = BitmapFactory.decodeResource(getResources(), R.drawable.right_down_connected);
                    }
                }



                else {
                    b = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder2); //
                }



                canvas.drawBitmap(b, null,new Rect(col * sizeSq, row * sizeSq,col*sizeSq+sizeSq, row*sizeSq+sizeSq), null);

                TextPaint tp = new TextPaint();
                tp.setColor(Color.GREEN);
                tp.setTextSize(70);
                tp.setTypeface(Typeface.create("Courier", Typeface.BOLD));
                canvas.drawText("Moves: " + String.valueOf(turns), 10, 1180, tp);


            }


        }



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        int column = getColTouched(event.getX());
        int row = getRowTouched(event.getY());


        try {
            mGame.rotateRight(column, row);
            System.out.println(mcolumns);



            mGame.checkWin();

            if (mGame.checkWin()) {
                System.out.println("check win works");
                invalidate();




            }
            invalidate();




        }
        catch (ArrayIndexOutOfBoundsException err) {
            System.out.println("User has pressed outside game grid - exception caught");
        }

        turns++;

        return super.onTouchEvent(event);
    }

    public int getColTouched(float x) {
        return (int) (x / sizeSq);
    }

    public int getRowTouched(float y) {
        return (int) (y / sizeSq);
    }

    public int getTurns() {
        return turns;
    }


}

MainActivity:

public class MainActivity extends AppCompatActivity {
    private int mcolumns;
    private int mrows;

    DatabaseHelper myDb;

    String test = "test data";
    int turns;

    static Context appcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        appcon = this;
        myDb = new DatabaseHelper(this);
    }

    public void runGame(View view){

        Intent intent = new Intent(this, GameViewActivity.class);

        startActivity(intent);
    }

    public void runInstructions(View view) {

        Intent intent = new Intent(this, InstructionsActivity.class);

        startActivity(intent);
    }


    public void setTurns() {
        GameView mGameView = new GameView(getApplicationContext());
        this.turns = mGameView.turns;
        System.out.println("Turns: " + Integer.toString(turns));
    }

    public void AddData() {
        boolean isInserted = myDb.insertData(Integer.toString(turns));
        if(isInserted == true) {
            System.out.println("Data inserted");
        }
        else {
            System.out.println("Data NOT inserted");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

MainAcivitysetTurns()

替换此

this.turns = mGameView.turns; //you can't access this variable directly 

this.turns = mGameView.getTurns(); // Calling the public method getTurns()

有关Java中访问修饰符的说明,请参阅此answer

答案 1 :(得分:0)

有两种方法可以解决这个问题。

<强> 1。首选方式:

GameView

中添加新方法
public int getTurns() {
    return turns;
}

使用此方法可以访问以下内容:

this.turns = mGameView.getTurns();

<强> 2。糟糕的方式: 将变量公开。