我在PositiveButton.setBackgroundColor上遇到了一个用户(三星,Android 5)的崩溃。它只发生过一次,我不能重复它。 这是代码:
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
activity = getActivity();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setCustomTitle(layout);
alertDialogBuilder.setView(returnDialogView());
alertDialogBuilder.setPositiveButton(R.string.btn_save, null);
mAlertDialog = alertDialogBuilder.create();
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface d) {
mPositiveButton = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
mPositiveButton.setBackgroundColor(getResources().getColor(R.color.brand_gray));
mPositiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(customerEditMode){
showFieldsOverCustomer();
}else {
save();
}
}
});
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
...
public void afterTextChanged(Editable s)
{
if (mInputCardNumber.getText().toString().equals("")){
mPositiveButton.setBackgroundColor(getResources().getColor(R.color.brand_gray)); //Here is the NullPointer Exception
private View returnDialogView() {
...
mInputCardNumber.addTextChangedListener(mTextEditorWatcher);
}
代码工作正常,没有人遇到问题。这是例外:
Caused by java.lang.NullPointerException
my.package.ui.fragment.dialog.NewTransactionDialogFragment$2.afterTextChanged (NewTransactionDialogFragment.java:275)
android.widget.TextView.sendAfterTextChanged (TextView.java:8699)
android.widget.TextView.setText (TextView.java:4701)
android.widget.TextView.setText (TextView.java:4545)
android.widget.EditText.setText (EditText.java:101)
android.widget.TextView.setText (TextView.java:4520)
...
在解除对话框之前,该按钮永远不会出现。我不知道它为什么会发生。我该如何解决呢。