2012-02-08 83 views
0

我正在開發一款android棋盤遊戲,並且遇到來自我的某個參數的錯誤。這是一個基於mancala的遊戲,其視圖(TableView.class)包含當時的排坑和石塊,以及主要活動(Game.class)。我正在嘗試定義一個坑及其內容。使用Java進行Android遊戲開發:無法解析參數

下面是代碼片段:

public class Game extends Activity { 

int pitIndex, pitContents; 
int _pitContents=4; 
int pitsPerRow=5; 


//more code here ....... 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     table_view = getInitialTableView(); 
     tableView = new TableView(this); 
     setContentView(tableView); 
     tableView.requestFocus(); 
//...more code here 
// 

// ... 


// define a playing pit 
public int Pit(int pitIndex, int pitContents) { 
this.pitIndex=pitIndex; 
this.pitContents=pitContents; 

} 

// set player1's pits and populate them 
public int Player1Pits[] = new int[16*2]; 
    { 
     for (int i = 0; i < (2 * pitsPerRow); i++) { 
      if (i < pitsPerRow) { 
       Player1Pits[i] = new Pit(i,_pitContents); 
      else { 
// do nothing 
} 
     } 
    } 
//..... 
} 

我從的Eclipse得到的錯誤是「坑不能被解析爲一個類型」當我嘗試實例化一個新坑:

Player1Pits[i] = new Pit(i,_pitContents); 

有人知道我要去哪裏嗎?我是否必須將Pit定義爲Game課程之外的課程? 在發佈此問題之前,我已經詳盡搜索瞭解決方案。您的意見將不勝感激。提前致謝。

+0

請插入你的logcat。 – iSun 2012-02-08 13:29:14

+0

@Ali。感謝您的迴應。截至目前,我沒有logcat。由於這個錯誤,程序不會編譯。 – Buzz 2012-02-08 13:37:01

+0

你確定你知道你在做什麼? – Shaiful 2012-02-08 13:41:04

回答

0

擺脫你的方法被稱爲坑,使一個坑班是這樣的:

public class Pit { 
    public int mPitIndex, mPitContents; 

    public Pit(int pitIndex, int pitContents) { 
     this.mPitIndex = pitIndex; 
     this.mPitContents = pitContents; 
    } 
} 

是什麼語言,你的方式熟悉?

+0

感謝Ian Newson.That解決了這個問題。我熟悉Matlab和VisualBasic,最近開始與Java。現在我想添加一個字符串到Pit類,但是當我這樣做時,我得到的錯誤「類型不匹配。不能從坑轉換爲字符串」。我如何重新定義我的數組(公共int Player1Pits [] = new int [16 * 2];),以便它接受除pitIndex和pitContents之外的字符串(pitString)?我嘗試過使用Array,但沒有奏效。 – Buzz 2012-02-08 15:06:48

0

是在'更多代碼'部分的某個地方坑嗎?從我看到的,你試圖直接在功能上做新的事,而不是在一個班上。

+0

其實他創建了一個'Pit'方法(在中間部分)並嘗試創建該方法的對象..:p – Shaiful 2012-02-08 13:53:06

+0

@ Ernstsson.Thanks for your contribution。我該怎麼做才能使它工作?做一個Pit類? – Buzz 2012-02-08 14:04:57