2017-02-19 61 views
0

您好,我有一個問題,從我的主要活動訪問我的視圖類中的變量/方法。我只需要返回來自GameView.java的turns int並能夠在MainActivity中訪問它,然後將其插入到我的數據庫中。我嘗試使用GameView mGameView = new GameView(getApplicationContext());,但這不起作用,我無法使用此方法從GameView訪問任何方法。Setter/Getter方法不起作用

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

使用mGameView.getTurns()將該類的轉角添加到mainactivity中的變量 – Tacolibre

回答

1

MainAcivity,裏面setTurns()

替換此

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

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

請參閱本answer進行說明有關訪問修飾符在Java中

+0

謝謝,但是使用這個會導致我的程序在調用此方法時崩潰。 – JB2000

+0

@ JB2000崩潰後的錯誤日誌是什麼? –

+0

http://pastebin.com/4zRiEvSB – JB2000

0

有兩種方法來解決這個問題。

1.首選方式:

添加一個新的方法GameView

public int getTurns() { 
    return turns; 
} 

而且要輪流訪問喜歡用這種方法:

this.turns = mGameView.getTurns();

2.壞道: 讓公衆變得可變。