简体   繁体   English

如何让程序显示吐司而不是崩溃?

[英]How to make a program show a toast instead of crashing?

I am working on an app.我正在开发一个应用程序。 Here's a small fragment which converts the entered string to a fraction:这是一个将输入的字符串转换为分数的小片段:

public int[] fractionalize(String rawFraction1) {
        int[] result = {0,0,0};
        if (rawFraction1.contains("/") && !rawFraction1.contains(" ")){
            //normal fraction
            int a = Integer.parseInt(rawFraction1.split("/")[0]);
            int b = Integer.parseInt(rawFraction1.split("/")[1]);
            result = new int[]{a,b,0};
        } else if (rawFraction1.contains("/") && rawFraction1.contains(" ")){
            //mixed fraction
            int wholeNum = Integer.parseInt(rawFraction1.split(" ")[0]);
            int num = Integer.parseInt(rawFraction1.split(" ")[1].split("/")[0]);
            int den = Integer.parseInt(rawFraction1.split(" ")[1].split("/")[1]);
            result = new int[]{num, den, wholeNum};
        } else if (!rawFraction1.contains(" ") && !rawFraction1.contains("/")){
            int wholeNum = Integer.parseInt(rawFraction1);
        } else {
            Toast toast = Toast.makeText(getApplicationContext(), "The number you entered is in an invalid format. Please see the correct format to enter numbers.",Toast.LENGTH_LONG);
            toast.show();
        }
        return result;
    }

This works pretty well, provided you enter the number in the correct format.如果您以正确的格式输入数字,这非常有效。 However, if we mess up the format, example, inserted 2 spaces or '/', the app crashes and closes.但是,如果我们弄乱了格式,例如插入 2 个空格或“/”,应用程序就会崩溃并关闭。 I want that if the program encounters a problem in this segment, it should show a toast instead and not close.我希望如果程序在此段中遇到问题,它应该显示一个 toast 而不是关闭。

Is this possible?这可能吗? If yes, how?如果是,如何?

You could check the string array length after splitting to show Toast.您可以在拆分后检查字符串数组长度以显示 Toast。 Another solution is use try/catch, and in catch block you show Toast (not recommend)另一种解决方案是使用 try/catch,并在 catch 块中显示 Toast(不推荐)

Just surround it with try/catch as easy as:只需使用 try/catch 将其包围,就像:

    public int[] fractionalize(String rawFraction1) {
        try {
            int[] result = {0, 0, 0};
            if (rawFraction1.contains("/") && !rawFraction1.contains(" ")) {
                //normal fraction
                int a = Integer.parseInt(rawFraction1.split("/")[0]);
                int b = Integer.parseInt(rawFraction1.split("/")[1]);
                result = new int[]{a, b, 0};
            } else if (rawFraction1.contains("/") && rawFraction1.contains(" ")) {
                //mixed fraction
                int wholeNum = Integer.parseInt(rawFraction1.split(" ")[0]);
                int num = Integer.parseInt(rawFraction1.split(" ")[1].split("/")[0]);
                int den = Integer.parseInt(rawFraction1.split(" ")[1].split("/")[1]);
                result = new int[]{num, den, wholeNum};
            } else if (!rawFraction1.contains(" ") && !rawFraction1.contains("/")) {
                int wholeNum = Integer.parseInt(rawFraction1);
            } else {
                Toast toast = Toast.makeText(getApplicationContext(), "The number you entered is in an invalid format. Please see the correct format to enter numbers.", Toast.LENGTH_LONG);
                toast.show();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Toast toast = Toast.makeText(getApplicationContext(), "Invalid Input.", Toast.LENGTH_LONG);
            toast.show();
        }
        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM