Ошибка TextWatcher NullPointerException
Я работаю над приложением для Android, чтобы составить список акций с помощью recyclerview и выполнить некоторые вычисления с помощью edittext. У меня есть проблема, которую я не могу решить. Пожалуйста, помогите мне.
Примечание: я знаю, что такое NullPointerException. Я могу убежать от NullPointerException, чтобы использовать разные способы, но мне нужно выяснить это, чтобы сделать процесс расчета в приложении.
Вот моя ошибка:
04-10 08:27:56.947 5198-5198/com.example.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.example, PID: 5198
java.lang.NullPointerException
at com.example.example.model.adapter.StockAdapter$QuantityTextListener.afterTextChanged(StockAdapter.java:199)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7424)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9194)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
at android.widget.TextView.doKeyDown(TextView.java:5532)
at android.widget.TextView.onKeyDown(TextView.java:5343)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:7665)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Metho
Мой EditText:
<EditText
android:id="@+id/edt_pruduct_amount"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_marginStart="25dp"
android:background="#d7d7d7"
android:ems="10"
android:gravity="center"
android:layout_gravity="start|center"
android:inputType="number"
android:text=""
android:textColor="#116f57"
android:textSize="18sp" />
И мой код в пользовательском адаптере:
onBindViewHolder:
DecimalFormat df = new DecimalFormat("0.00##");
Stock currStock = mStocks.get(position);
holder.edtProductAmount.setTag(currStock);
if (currStock.getQuantity() != 0) {
holder.edtProductAmount.setText(String.valueOf(currStock.getQuantity()));
} else {
holder.edtProductAmount.setText("");
}
...
holder.edtProductAmount.addTextChangedListener(new QuantityTextListener(this, holder.edtProductAmount));
Внутренний класс TextWatcher:
public class QuantityTextListener implements TextWatcher {
private View view;
private QuantityTextListener(StockAdapter stockAdapter, View view) {
this.view = view;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("0.00##");
String qtyString = s.toString().trim();
CheckBox cbU1 = view.findViewById(R.id.cb_unit_one_product_name);
CheckBox cbU2 = view.findViewById(R.id.cb_unit_two_product_name);
CheckBox cbU3 = view.findViewById(R.id.cb_unit_three_product_name);
CheckBox cbU4 = view.findViewById(R.id.cb_unit_four_product_name);
Button btnAddToCart = view.findViewById(R.id.btn_add_to_cart);
final int quantity = qtyString.equals("") ? 0 : Integer.valueOf(qtyString);
Log.d("Quantity: ", String.valueOf(quantity));
EditText qtyView = view.findViewById(R.id.edt_pruduct_amount);
final Stock listedStock = (Stock) qtyView.getTag();
if (listedStock.getQuantity() != quantity) {
if (cbU1.isChecked()) {
final Double u1TotalPrice = quantity * listedStock.getsUnit1Amount();
listedStock.setQuantity(quantity);
listedStock.setsUnit1TotalAmount(u1TotalPrice);
TextView tvU1TotalPrice = view.findViewById(R.id.tv_unit_one_product_price_total);
if (listedStock.getQuantity() != 0) {
tvU1TotalPrice.setText(df.format(listedStock.getsUnit1TotalAmount()) + "₺");
qtyView.setText(String.valueOf(listedStock.getQuantity()));
btnAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// add to paper
if (listedStock.getsId() != null || !listedStock.getsId().equals("")) {
Paper.book().write("ReturnStockId", listedStock.getsId());
}
if (listedStock.getsName() != null || !listedStock.getsName().equals("")) {
Paper.book().write("ReturnProductName", listedStock.getsName());
}
if (listedStock.getQuantity() > 0) {
Paper.book().write("ReturnProductQuantity", quantity);
}
if (listedStock.getsUnit1Amount() > 0) {
Paper.book().write("ReturnProductPrice", listedStock.getsUnit1Amount());
}
if (listedStock.getsUnit1TotalAmount() > 0) {
Paper.book().write("ReturnProductTotalPrice", u1TotalPrice);
}
Toast.makeText(mCtx, quantity + " " + listedStock.getsUnit1() + " " + listedStock.getsName() + " sepete eklendi.", Toast.LENGTH_SHORT).show();
}
});
} else {
tvU1TotalPrice.setText("");
qtyView.setText("");
// delete from paper
}
}
if (cbU2.isChecked()) {
final Double u2TotalPrice = quantity * listedStock.getsUnit1Amount();
listedStock.setQuantity(quantity);
listedStock.setsUnit1TotalAmount(u2TotalPrice);
TextView tvU2TotalPrice = view.findViewById(R.id.tv_unit_two_product_price_total);
if (listedStock.getQuantity() != 0) {
tvU2TotalPrice.setText(df.format(listedStock.getsUnit1TotalAmount()) + "₺");
qtyView.setText(String.valueOf(listedStock.getQuantity()));
btnAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// add to paper
if (listedStock.getsId() != null || !listedStock.getsId().equals("")) {
Paper.book().write("ReturnStockId", listedStock.getsId());
}
if (listedStock.getsName() != null || !listedStock.getsName().equals("")) {
Paper.book().write("ReturnProductName", listedStock.getsName());
}
if (listedStock.getQuantity() > 0) {
Paper.book().write("ReturnProductQuantity", quantity);
}
if (listedStock.getsUnit2Amount() > 0) {
Paper.book().write("ReturnProductPrice", listedStock.getsUnit2Amount());
}
if (listedStock.getsUnit2TotalAmount() > 0) {
Paper.book().write("ReturnProductTotalPrice", u2TotalPrice);
}
Toast.makeText(mCtx, quantity + " " + listedStock.getsUnit2() + " " + listedStock.getsName() + " sepete eklendi.", Toast.LENGTH_SHORT).show();
}
});
} else {
tvU2TotalPrice.setText("");
qtyView.setText("");
}
}
}
}
}
Ошибка отображается в этой строке (StockAdapter.java:342):
if (cbU1.isChecked()) {...