您好我正在尝试构建一个计算器来计算复合兴趣,我不断收到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
答案 0 :(得分:1)
double capital = Double.parseDouble(edt1.getText().toString());
发生在你的活动的初始阶段,这在你致电edt1 = (EditText)findViewById(R.id.edt1);
之前就已经发生了。
您可以在类的隐式init中声明双重大写...但是对edt1的引用必须在调用onCreate
之后。
此外,edt1.getText
的实际内容即使在此时也不相关,因为它是空的,因此也会导致异常。
请注意,在onCreate方法下声明变量并不会在onCreate之后调用它。
我认为你不需要这四行:
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());
答案 1 :(得分: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());