1

请检查给定的代码,我在 C++ 中使用了相同的代码,并进行了细微的更改,它运行得很好。我也期待着为 android 制作它。但是我只是按下计算按钮就崩溃了。请告诉我这里有什么问题。PS我是初学者。

我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Optimum Tilt Angle Calculator"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Calculate" />

<TextView
    android:id="@+id/beta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Beta = "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/rad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="H = "
    android:textAppearance="?android:attr/textAppearanceLarge" />


我的 Tilt.java:

    package com.ned.tilt;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class Tilt extends Activity implements android.view.View.OnClickListener {
private static final double PI = 3.142;
private static final double PHI = 24.8508 * (PI / 180);
private static final double Gsc = 1367;
private static final double RO = 0.2;
int i, beta, ang;

double delta, ws, wss, Hom[], Gon[], Kt[], Hd[], Rb, Rb_num, Rb_den, Ra,
        Htm, Ht;
int nbar[] = { 17, 47, 75, 105, 135, 162, 198, 228, 258, 288, 318, 344 };
int N[] = { 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
double Hm[] = { 4.38, 5.18, 5.93, 6.65, 6.67, 6.40, 5.44, 5.27, 5.62, 5.24,
        4.5, 4.11 };
Button cal;
TextView radiation, angle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initialize();
    cal.setOnClickListener(this);

}

private void initialize() {
    // TODO Auto-generated method stub
    cal = (Button) findViewById(R.id.button1);
    radiation = (TextView) findViewById(R.id.rad);
    angle = (TextView) findViewById(R.id.beta);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    for (i = 0; i <= 11; i++) {
        Htm = 0;
        delta = 23.45 * (Math.sin((0.986301369) * (PI / 180)
                * (284 + (nbar[i]))));

        ws = Math.acos(-(Math.tan(PHI)) * (Math.tan(delta * (PI / 180))));
        ws = ws * (180 / PI);

        Gon[i] = Gsc
                * (1 + (0.033 * Math.cos(0.986301369 * (nbar[i])
                        * (PI / 180))));

        Hom[i] = (86400 / PI)
                * Gon[i]
                * (((Math.cos(PHI)) * (Math.cos(delta * (PI / 180))) * (Math
                        .sin(ws * (PI / 180)))) + ((ws * (PI / 180)) * (Math
                        .sin(PHI) * (Math.sin(delta * (PI / 180))))));
        Hom[i] = Hom[i] / 3600000;

        Kt[i] = Hm[i] / Hom[i];

        Hd[i] = (0.96268) - ((1.45200) * (Kt[i]))
                + ((0.27365) * (Kt[i]) * (Kt[i]))
                + ((0.04279) * (Kt[i]) * (Kt[i]) * (Kt[i]))
                + ((0.000246) * (ws))
                + ((0.001189) * (90 - (PHI * (180 / PI)) + delta));
        Hd[i] = Hd[i] * Hm[i];
        for (beta = 0; beta <= 90; beta++) {
            wss = Math.acos(-(Math.tan((PHI) - (beta * (PI / 180))) * (Math
                    .tan(delta * (PI / 180)))));
            wss = wss * (180 / PI);
            if (wss > ws) {
                wss = ws;
            }

            Rb_num = ((Math.cos((PHI) - (beta * (PI / 180))))
                    * (Math.cos(delta * (PI / 180))) * (Math.sin(wss
                    * (PI / 180))))
                    + (((PI / 180) * wss)
                            * ((Math.sin((PHI) - (beta * (PI / 180))))) * ((Math
                                .sin(delta * (PI / 180)))));
            Rb_den = ((Math.cos(PHI)) * (Math.cos(delta * (PI / 180))) * (Math
                    .sin(ws * (PI / 180))))
                    + (((PI / 180) * (ws) * (Math.sin(PHI)) * (Math
                            .sin(delta * (PI / 180)))));
            Rb = Rb_num / Rb_den;
            Ra = ((1 - (Hd[i] / Hm[i])) * (Rb))
                    + ((Hd[i] / (2 * Hm[i])) * (1 + (Math.cos(beta
                            * (PI / 180)))))
                    + ((RO / 2) * (1 - (Math.cos(beta * (PI / 180)))));

            Ht = Ra * Hm[i];
            if (Ht > Htm) {
                Htm = Ht;
                ang = beta;
            }
        }

        break;
    }
    radiation.setText("H = " + Htm);
    angle.setText("Beta =" + ang);

}
 }

日志猫:

02-28 17:25:10.344: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.344: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.344: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.434: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.434: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.894: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.894: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.924: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:10.944: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.144: D/libEGL(773): loaded /system/lib/egl/libEGL_emulation.so
02-28 17:25:11.154: D/(773): HostConnection::get() New Host Connection established 0x2a186bf0, tid 773
02-28 17:25:11.184: D/libEGL(773): loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-28 17:25:11.193: D/libEGL(773): loaded /system/lib/egl/libGLESv2_emulation.so
02-28 17:25:11.335: W/EGL_emulation(773): eglSurfaceAttrib not implemented
02-28 17:25:11.383: D/OpenGLRenderer(773): Enabling debug mode 0
02-28 17:25:11.394: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.403: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.415: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.444: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.444: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.464: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.464: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.464: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.464: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.474: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.514: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.524: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.524: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.524: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.724: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.774: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:25:11.864: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.747: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.747: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.747: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.767: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.767: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.767: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.777: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.777: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.910: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:49.910: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.397: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.397: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.473: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.473: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.473: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.477: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.477: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.477: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.477: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.477: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.497: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.497: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.497: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.497: W/Trace(773): Unexpected value from nativeGetEnabledTags: 0
02-28 17:26:51.527: D/AndroidRuntime(773): Shutting down VM
02-28 17:26:51.527: W/dalvikvm(773): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-28 17:26:51.537: E/AndroidRuntime(773): FATAL EXCEPTION: main
02-28 17:26:51.537: E/AndroidRuntime(773): java.lang.NullPointerException
02-28 17:26:51.537: E/AndroidRuntime(773):  at com.ned.tilt.Tilt.onClick(Tilt.java:53)
02-28 17:26:51.537: E/AndroidRuntime(773):  at android.view.View.performClick(View.java:4202)
02-28 17:26:51.537: E/AndroidRuntime(773):  at android.view.View$PerformClick.run(View.java:17340)
02-28 17:26:51.537: E/AndroidRuntime(773):  at android.os.Handler.handleCallback(Handler.java:725)
02-28 17:26:51.537: E/AndroidRuntime(773):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 17:26:51.537: E/AndroidRuntime(773):  at android.os.Looper.loop(Looper.java:137)
02-28 17:26:51.537: E/AndroidRuntime(773):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-28 17:26:51.537: E/AndroidRuntime(773):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 17:26:51.537: E/AndroidRuntime(773):  at java.lang.reflect.Method.invoke(Method.java:511)
02-28 17:26:51.537: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 17:26:51.537: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 17:26:51.537: E/AndroidRuntime(773):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您没有初始化 Hom[]、Gon[]、Kt[]、Hd[] 数组,但您正在尝试访问并在循环中设置它。一个例子是这样的:

    Hd[i] = Hd[i] * Hm[i];

由于您正在处理长度为 12 的循环,因此您可以为它们初始化空数组。

于 2013-02-28T17:38:53.000 回答