我的代码出了什么问题? (执行代码时崩溃)

时间:2012-01-01 22:35:31

标签: java android

它没有错误,但是当我运行它时,它会崩溃。我认为问题所在的代码部分如下:

    int h = (a * c);
    List<Integer> factors = new ArrayList<Integer>();
    for (int i = 1; i < Math.sqrt(h); i++) {
        if (h % i == 0)
            factors.add(i);
    }
    Integer result = null;
    for (int ii : factors) {
        if (b == ii + h / ii) {
            result = ii;
            // ax^2+hiix+iix+c
        }
        int hii = h / ii;
        int gcd1 = Euclid.getGcd(a, hii);
        int gcd2 = Euclid.getGcd(ii, c);
        String factored1 = ("(" + gcd1 + "x+" + gcd2 + ")");
        int fact21 = (a / gcd1);
        int fact22 = (hii / gcd1);
        String factored2 = ("(" + fact21 + "x+" + fact22 + ")");
        EditText tvfact1a = (EditText) findViewById(R.id.tvfact1);
        EditText tvfact2a = (EditText) findViewById(R.id.tvfact2);
        tvfact1a.setText(factored1);
        tvfact2a.setText(factored2);
    }

a和c在程序的早期初始化。欧几里德类如下:

package boston.project;

public class Euclid {
    public static int getGcd(int a, int b) {
        while (b != 0) {
            if (a > b) {
                a -= b;
            } else {
                b -= a;
            }
        }
        return a;
    }
}

修改

logcat的

01-01 15:28:59.479:W / KeyCharacterMap(303):没有用于id 0的键盘 01-01 15:28:59.479:W / KeyCharacterMap(303):使用默认键映射:/system/usr/keychars/qwerty.kcm.bin
01-01 16:22:03.158:W / KeyCharacterMap(334):没有用于id 0的键盘 01-01 16:22:03.158:W / KeyCharacterMap(334):使用默认键映射:/system/usr/keychars/qwerty.kcm.bin
01-01 16:22:16.739:D / AndroidRuntime(334):关闭VM
01-01 16:22:16.739:W / dalvikvm(334):threadid = 1:线程退出未捕获异常(组= 0x4001d800)
01-01 16:22:16.759:E / AndroidRuntime(334):致命异常:主要是 01-01 16:22:16.759:E / AndroidRuntime(334):java.lang.ClassCastException:android.widget.TextView
01-01 16:22:16.759:E / AndroidRuntime(334):at boston.project.TheBostonProjectActivity $ 1.onClick(TheBostonProjectActivity.java:138)
01-01 16:22:16.759:E / AndroidRuntime(334):在android.view.View.performClick(View.java:2408)
01-01 16:22:16.759:E / AndroidRuntime(334):在android.view.View $ PerformClick.run(View.java:8816)
01-01 16:22:16.759:E / AndroidRuntime(334):在android.os.Handler.handleCallback(Handler.java:587)
01-01 16:22:16.759:E / AndroidRuntime(334):在android.os.Handler.dispatchMessage(Handler.java:92)
01-01 16:22:16.759:E / AndroidRuntime(334):在android.os.Looper.loop(Looper.java:123)
01-01 16:22:16.759:E / AndroidRuntime(334):在android.app.ActivityThread.main(ActivityThread.java:4627)
01-01 16:22:16.759:E / AndroidRuntime(334):at java.lang.reflect.Method.invokeNative(Native Method)
01-01 16:22:16.759:E / AndroidRuntime(334):at java.lang.reflect.Method.invoke(Method.java:521)
01-01 16:22:16.759:E / AndroidRuntime(334):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
01-01 16:22:16.759:E / AndroidRuntime(334):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-01 16:22:16.759:E / AndroidRuntime(334):at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

我认为错误的发生是因为您在布局中使用TextView但尝试将其转换为代码中的EditText。 你应该改变

EditText tvfact1a = (EditText) findViewById(R.id.tvfact1);
EditText tvfact2a = (EditText) findViewById(R.id.tvfact2);

TextView tvfact1a = (TextView) findViewById(R.id.tvfact1);
TextView tvfact2a = (TextView) findViewById(R.id.tvfact2);

答案 1 :(得分:-2)

问题可能出在

String factored1 = ("(" + gcd1 + "x+" + gcd2 + ")");

尝试将gcd1gcd2明确更改为字符串,如下所示:

String factored1 = ("(" + Integer.toString(gcd1) + "x+" + Integer.toString(gcd2) + ")");