1

嗨,我正在尝试构建一个计算器来计算复利,但我不断收到java.lang.RuntimeException错误消息。我已经阅读了一些关于 stackoverflow 的主题,他们的问题似乎与他们的 AndroidManifest.xml 有问题。我根本没有编辑该文件,但我仍然收到此错误。这是代码:

 package com.hychentsa.compoundinterest_v001;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import java.lang.Math;


    public class MainActivity extends Activity {
        private Button btn1;
        private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

        btn1 = (Button)findViewById(R.id.btn1);

        edt1 = (EditText)findViewById(R.id.edt1);
        edt2 = (EditText)findViewById(R.id.edt2);
        edt3 = (EditText)findViewById(R.id.edt3);
        edt4 = (EditText)findViewById(R.id.edt4);
        edt5 = (EditText)findViewById(R.id.edt5);
        edt6 = (EditText)findViewById(R.id.edt6);

        btn1.setOnClickListener(btn1listener);
    }
    double capital = Double.parseDouble(edt1.getText().toString());
    double everyyear = Double.parseDouble(edt2.getText().toString());
    double interest = Double.parseDouble(edt3.getText().toString());
    double year = Double.parseDouble(edt4.getText().toString());

    private Button.OnClickListener btn1listener = new Button.OnClickListener()
    {
        public void onClick (View v)
        {
            double capital = Double.parseDouble(edt1.getText().toString());
            double everyyear = Double.parseDouble(edt2.getText().toString());
            double interest = Double.parseDouble(edt3.getText().toString());
            double year = Double.parseDouble(edt4.getText().toString());
            double interestperyear = capital * Math.pow(1 + interest, year) + 
                    everyyear * (1 - Math.pow(1 + interest, year)) / interest * -1;
            edt5.setText(String.valueOf(interestperyear));
            edt6.setText(String.valueOf(interestperyear / 12));
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是logcat:

07-15 13:45:23.640: D/AndroidRuntime(1763): Shutting down VM
07-15 13:45:23.650: W/dalvikvm(1763): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-15 13:45:23.660: E/AndroidRuntime(1763): FATAL EXCEPTION: main
07-15 13:45:23.660: E/AndroidRuntime(1763): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hychentsa.compoundinterest_v001/com.hychentsa.compoundinterest_v001.MainActivity}: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.os.Looper.loop(Looper.java:137)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at dalvik.system.NativeStart.main(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): Caused by: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763):     at com.hychentsa.compoundinterest_v001.MainActivity.<init>(MainActivity.java:30)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.Class.newInstanceImpl(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.Class.newInstance(Class.java:1319)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-15 13:45:23.660: E/AndroidRuntime(1763):     ... 11 more
4

2 回答 2

1
double capital = Double.parseDouble(edt1.getText().toString());

发生在您的活动的初始阶段,这在您调用之前edt1 = (EditText)findViewById(R.id.edt1);

您可以在类的隐式初始化中声明您的双大写 ...,但必须在调用 edt1 之后onCreate调用。

此外,即使在这一点上,实际内容edt1.getText也不相关,因为它是空的,因此也会导致异常。

请注意,在 onCreate 方法下声明变量不会使其在 onCreate 之后被调用。

我认为你不需要这 4 行:

double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
于 2013-07-15T13:56:08.723 回答
1

删除按钮侦听器之前的以下内容。这会导致 NPE。它在方法之外。您也有相同的内部按钮单击,因此您可以删除方法之外的按钮。

double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
于 2013-07-15T14:00:40.973 回答