首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >怎么了--河内的高楼

怎么了--河内的高楼
EN

Stack Overflow用户
提问于 2011-05-07 03:45:29
回答 1查看 339关注 0票数 0

我能解释一下为什么当我在android模拟器上部署这个代码时,它不能工作吗?

代码语言:javascript
复制
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();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-05-07 04:24:45

我将假设您的布局在editText1android:onClick属性中声明了myClickHandler,但是最好包括您的布局,以防止人们不得不猜测。例如,如果onClick处理程序连接不正确或根本没有连接,您将收到另一个异常,否则什么都不会发生。

但我将假设该方法已被正确调用。您试图在没有指定路径的情况下创建一个文件,因此它尝试打开/TowersOfHanoiSolution.txt并接收一个FileNotFoundException,其原因是“只读文件系统”:

代码语言:javascript
复制
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或LogCat窗口,查看异常是什么;关键信息是引用代码的第一个行号。

请注意,这不是主要的异常;主要的异常是框架抱怨onClick调用失败。同样,这也是为什么在调试器中暂停执行可能过于复杂的原因,而只需读取logcat来查看所有异常是什么更容易。

不管怎样:你不能在Android上的/中创建文件;因为那是工作目录,你需要指定完整的路径(我假设是到SD卡的)。如下所示:

代码语言:javascript
复制
FileOutputStream fos = new FileOutputStream(new File(
    Environment.getExternalStorageDirectory(),
    "TowersOfHanoiSolution.txt"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5916287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档