文本更改侦听器上的值出现空指针异常 android
Null pointer exception with values on text change listener android
我有简单计算复利的代码。当用户在 EditText 视图中编辑值时,计算将发生变化。
public class CompoundInterest extends AppCompatActivity {
TextView mCompBalance;
TextView mBalance;
EditText mYears;
Integer balance;
Integer years;
Double compbalance;
Double interest;
boolean isNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compound_interest);
interest = 0.025;
mBalance = (TextView) findViewById(R.id.tv_savingsbalance);
mCompBalance = (TextView) findViewById(R.id.tv_interestbalance);
mYears = (EditText) findViewById(R.id.et_years);
System.out.println(mYears.getText().toString());
Resources res = getResources();
balance = Integer.valueOf(res.getString(R.string.savings_balance));
mBalance.setText("£"+ balance);
mYears.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (mYears.getText().toString().matches("\d+(?:\.\d+)?")){
years = Integer.valueOf(mYears.getText().toString());
System.out.println(years);
isNumber =true;
} else {
isNumber =false;
}
if (isNumber){
compbalance = balance*Math.pow(1+interest, years);
mCompBalance.setText("£"+String.format("%.2f", compbalance));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
当我尝试编辑设置计算年数的 EditText 视图时 - 我收到此错误并且应用程序崩溃:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.###.android.citiapp.CompoundInterest.onTextChanged(CompoundInterest.java:63)
at android.widget.TextView.sendOnTextChanged(TextView.java:8187)
at android.widget.TextView.handleTextChanged(TextView.java:8249)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10371)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:230)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:229)
at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:251)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:459)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:93)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我不确定发生了什么。
在 onTextChanged
函数中使用 charSequence.toString()
而不是 mYears.getText().toString()
。希望它有所帮助或具体说明 NullPointerException
发生的位置?
我有简单计算复利的代码。当用户在 EditText 视图中编辑值时,计算将发生变化。
public class CompoundInterest extends AppCompatActivity {
TextView mCompBalance;
TextView mBalance;
EditText mYears;
Integer balance;
Integer years;
Double compbalance;
Double interest;
boolean isNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compound_interest);
interest = 0.025;
mBalance = (TextView) findViewById(R.id.tv_savingsbalance);
mCompBalance = (TextView) findViewById(R.id.tv_interestbalance);
mYears = (EditText) findViewById(R.id.et_years);
System.out.println(mYears.getText().toString());
Resources res = getResources();
balance = Integer.valueOf(res.getString(R.string.savings_balance));
mBalance.setText("£"+ balance);
mYears.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (mYears.getText().toString().matches("\d+(?:\.\d+)?")){
years = Integer.valueOf(mYears.getText().toString());
System.out.println(years);
isNumber =true;
} else {
isNumber =false;
}
if (isNumber){
compbalance = balance*Math.pow(1+interest, years);
mCompBalance.setText("£"+String.format("%.2f", compbalance));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
当我尝试编辑设置计算年数的 EditText 视图时 - 我收到此错误并且应用程序崩溃:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.###.android.citiapp.CompoundInterest.onTextChanged(CompoundInterest.java:63) at android.widget.TextView.sendOnTextChanged(TextView.java:8187) at android.widget.TextView.handleTextChanged(TextView.java:8249) at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10371) at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578) at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:230) at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:229) at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:251) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:459) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:93) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我不确定发生了什么。
在 onTextChanged
函数中使用 charSequence.toString()
而不是 mYears.getText().toString()
。希望它有所帮助或具体说明 NullPointerException
发生的位置?