繁体   English   中英

Setter / Getter方法不起作用

[英]Setter/Getter method not working

嗨,我在从主要活动中访问视图类中的变量/方法时遇到问题。 我只需要从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;
    }


}

主要活动:

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");
        }
    }
}

MainAcivity setTurns()内部

取代这个

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

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

请参阅此答案以获取有关Java中的访问修饰符的解释

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

1.首选方式:

GameView类中添加新方法

public int getTurns() {
    return turns;
}

并在要访问转弯的地方使用此方法:

this.turns = mGameView.getTurns();

2.不好的方法:公开变数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM