当我添加此行时,不知道为什么会发生这种情况,它开始崩溃应用程序:
total = total + add;
tvMoney.setText("Your Total is $" + total);
以下是代码的完整部分,当我删除(或注释掉)上述代码时,它可以正常工作。应用程序应该更新一个部分,同时还更新具有总计的另一个文本。
// What happens when the "+" button is pressed.
public void sendAdd(View view) {
llMoney = (LinearLayout) findViewById(R.id.llMoney);
etMoney = (EditText) findViewById(R.id.etMoney);
String message = etMoney.getText().toString();
if (message == null || message.isEmpty()){
add = 0.0;
} else {
add = Double.parseDouble(etMoney.getText().toString());
}
total = total + add;
tvMoney.setText("Your Total is $" + total);
TextView text = new TextView(this);
text.setText("You've added money! + " + message);
text.setTextSize(20);
text.setTextColor(Color.GREEN);
text.setGravity(Gravity.RIGHT);
llMoney.addView(text);
以下是变量声明:
//declaring items
LinearLayout llNotes, llMoney;
Button butNotes, butAdd, butSub;
EditText etNotes, etMoney;
TextView tvMoney;
//To update the total amount of money variable
double total, add, sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
total = 0.0;
add = 0.0;
sub = 0.0;
logcat的
05-11 17:54:20.477: D/AndroidRuntime(23918): Shutting down VM
05-11 17:54:20.477: W/dalvikvm(23918): threadid=1: thread exiting with uncaught exception (group=0x4145c360)
05-11 17:54:20.497: E/AndroidRuntime(23918): FATAL EXCEPTION: main
05-11 17:54:20.497: E/AndroidRuntime(23918): java.lang.IllegalStateException: Could not execute method of the activity
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.view.View$1.onClick(View.java:3690)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.view.View.performClick(View.java:4192)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.view.View$PerformClick.run(View.java:17254)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.os.Handler.handleCallback(Handler.java:615)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.os.Looper.loop(Looper.java:137)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.app.ActivityThread.main(ActivityThread.java:4950)
05-11 17:54:20.497: E/AndroidRuntime(23918): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:54:20.497: E/AndroidRuntime(23918): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 17:54:20.497: E/AndroidRuntime(23918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-11 17:54:20.497: E/AndroidRuntime(23918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-11 17:54:20.497: E/AndroidRuntime(23918): at dalvik.system.NativeStart.main(Native Method)
05-11 17:54:20.497: E/AndroidRuntime(23918): Caused by: java.lang.reflect.InvocationTargetException
05-11 17:54:20.497: E/AndroidRuntime(23918): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:54:20.497: E/AndroidRuntime(23918): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 17:54:20.497: E/AndroidRuntime(23918): at android.view.View$1.onClick(View.java:3685)
05-11 17:54:20.497: E/AndroidRuntime(23918): ... 11 more
05-11 17:54:20.497: E/AndroidRuntime(23918): Caused by: java.lang.NullPointerException
05-11 17:54:20.497: E/AndroidRuntime(23918): at net.tagyoureit.heykeepup.MainActivity.sendAdd(MainActivity.java:123)
05-11 17:54:20.497: E/AndroidRuntime(23918): ... 14 more
答案 0 :(得分:0)
你没有初始化tvMoney。
如果你没有初始化一个成员变量(比如tvMoney)你的代码会编译,但你在运行时会遇到问题。
答案 1 :(得分:0)
您似乎没有在任何地方实例化您的tvMoney变量,如下所示:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMoney = (TextView) findViewById(R.id.tvMoney);
total = 0.0;
add = 0.0;
sub = 0.0;
答案 2 :(得分:0)
when I added this line it started crashing the app:
当您尝试设置文本时, tvMoney
为空。您需要在访问之前找到TextView
的实例。我相信TextView
中的tvMoney
ID为Activity layout
,然后更改
tvMoney = (EditText) findViewById(R.id.tvMoney);
tvMoney.setText("Your Total is $" + total);