开发者

Whats wrong-Towers of Hanoi

can some explain to me why this code wont work when i deploy it on the android emulator

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


I’m going to assume that your layout declares myClickHandler in the android:onClick attribute of editText1, but it’s best to include your layout to prevent people from having to guess. For example, if the onClick handler is connected incorrectly or not at all, you’ll receive another exception, or nothing will happen.

But I’ll assume the method is being called properly. You’re trying to create a file without specifying a path, so it tries to open /TowersOfHanoiSolution.txt and receives a FileNotFoundException, the cause of which is “Read-only file system”:

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

Are you using Eclipse? If you debug with Eclipse, you can get a stacktrace like the above in the logcat window, and it can also suspend execution to let you examine the state of your application. It can be simpler, however, just to watch adb logcat, or the LogCat window of Eclipse, to see what the exception was; the crucial information is the first line number that refers to your code.

Note that this was not the main exception; the main exception was the framework complaining that the onClick invocation failed. Again, this is why suspending execution in the debugger can be overly complicated, and it’s easier just to read the logcat to see what all the exceptions were.

Anyway: You can’t create files in / on Android; since that’s the working directory, you’ll want to specify the full path (to the SD card, I assume). Like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜