2011-05-06 57 views
0

可以給我一些解釋錯塔爲什麼當我在Android模擬器部署請告訴我河內

public class towers extends Activity { 

    /** Called when the activity is first created. */ 
    private EditText text; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     text = (EditText) findViewById(R.id.editText1); 
    } 

    static int moves = 0; 
    static int totalDisks = 0; 

    public void myClickHandler(View view) throws java.io.IOException { 
     char fromPole = 'A'; 
     char withPole = 'B'; 
     char toPole = 'C'; 

     switch (view.getId()) { 
      case R.id.editText1: 

       if (text.getText().length() == 0) { 
        Toast.makeText(this, "Please enter number of disks", 
          Toast.LENGTH_LONG).show(); 
        return; 
       } 
       float disks = Float.parseFloat(text.getText().toString()); 
       FileOutputStream fos = new FileOutputStream("TowersOfHanoiSolution.txt"); 
       PrintStream ps = new PrintStream(fos); 
       solveHanoi(disks, fromPole, toPole, withPole, ps); 
       ps.close(); 
       System.out.println(); 
       text.setText("\nAmount of moves: " + moves + "\n"); 
     } 

    } 

    static void solveHanoi(float disks, char start, char end, char intermediate, PrintStream ps) { 
     if (disks >= 1) { 
      solveHanoi(disks-1, start, intermediate, end, ps); 
      moveDisk(start, end, ps); 
      solveHanoi(disks-1, intermediate, end, start, ps); 
     } 
    } 

    static void moveDisk(char fromPole, char toPole, PrintStream ps) { 
     moves++; 

     if(totalDisks <= 10){ 
      System.out.print("Move from " + fromPole + " to " + toPole + ". "); 
      ps.print("Move from " + fromPole + " to " + toPole + ". "); 
      if (moves%4 == 0){ 
       System.out.println(); 
       ps.println(); 
      } 
     } 
     else { 
      ps.print("Move from " + fromPole + " to " + toPole + ". "); 
      if (moves%4 == 0){ 
       ps.println(); 
      } 
     } 
    } 
} 
+6

如果你解釋了你期望發生的事情和未發生的事情,它將會很有用。 – 2011-05-06 19:48:24

+2

首先,誰應該解決這個難題 - 用戶還是算法? – 2011-05-06 19:52:13

+0

我是第二個@約翰說的,而且你也需要提供你的佈局,因爲代碼至多是我們需要幫助你的75%。僅查看代碼,您沒有爲editText1設置點擊處理程序,因此,在單擊editText1時看起來您想要執行某些操作,但實際上並未將點擊處理程序鏈接到該控件。 – RivieraKid 2011-05-06 20:10:21

回答

1

我會假設你的佈局的屬性聲明myClickHandler這個代碼不會工作editText1,但最好包含您的佈局以防止人們猜測。例如,如果onClick處理程序連接不正確或根本沒有連接,則會收到另一個異常,或者什麼都不會發生。

但我會假設該方法被正確調用。你試圖建立一個沒有指定路徑的文件,因此它試圖打開/TowersOfHanoiSolution.txt並接收FileNotFoundException,火災原因是「只讀文件系統」:

Caused by: java.io.FileNotFoundException: /TowersOfHanoiSolution.txt (Read-only file system) 
    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:165) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:144) 
    at com.example.towers.towers.myClickHandler(towers.java:49) 
    ... 14 more 

你使用Eclipse?如果您使用Eclipse進行調試,則可以在logcat窗口中獲得類似上面的堆棧跟蹤,並且還可以掛起執行,讓您檢查應用程序的狀態。但是,只需要觀察adb logcat或Eclipse的LogCat窗口就可以更簡單地查看異常情況;關鍵信息是引用您的代碼的第一個行號。

請注意,這不是主要的例外;主要的例外是框架抱怨onClick調用失敗。再一次的,這就是爲什麼在調試器中暫停執行可能過於複雜的原因,並且讀取logcat以查看所有異常是什麼更容易。

無論如何:您無法在Android上的/中創建文件;因爲這是工作目錄,所以您需要指定完整路徑(我假設爲SD卡)。像這樣:

FileOutputStream fos = new FileOutputStream(new File(
    Environment.getExternalStorageDirectory(), 
    "TowersOfHanoiSolution.txt"));