空指针异常错误,用于Android addTextChangedListener。

huangapple go评论70阅读模式
英文:

NullPointerException Error for Android addTextChangedListener

问题

public class CreateNewOrder extends AppCompatActivity {
    //declaring dropdowns
    private Spinner spinner1, spinner2, spinner3;
    //declaring Text fields
    EditText txtSubtotal, txtGstamt, txtTotal, txtbalance, txtPrice, txtQty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_new_order_pg);

        //Hooks for txt fields
        txtQty = findViewById(R.id.etQty);
        txtPrice = findViewById(R.id.tvPrice);
        txtSubtotal = findViewById(R.id.tvSubtotal);
        txtGstamt = findViewById(R.id.tvGstamt);
        txtTotal = findViewById(R.id.tvTotalamt);
        txtamtpaid = findViewById(R.id.tvAmtpaid);
        txtbalance = findViewById(R.id.tvbalance);

        //Hook for subtotal Calculation
        txtPrice.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) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                double pric = Double.parseDouble(txtPrice.getText().toString());
                double quan = Double.parseDouble(txtQty.getText().toString());
                double subtot = quan * pric;
                txtSubtotal.setText((String.valueOf(subtot)));
            }
        });

以上是您提供的代码的翻译部分。

英文:

I am getting the below Null pointer error. please help be debug this.
I am getting the below Null pointer error. please help be debug this.

   
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.distributionlist.CreateNewOrder$1.afterTextChanged(CreateNewOrder.java:72)
at android.widget.TextView.sendAfterTextChanged(TextView.java:10551)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:13388)

This is my Layout: when I try to run the code it is giving me a nullpointerexception. Any help would be greatly appreciated.

I am getting the below Null pointer error. please help be debug this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="10dp"
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spItem"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:background="@drawable/spinner_background" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<EditText
android:id="@+id/tvQty"
android:layout_width="170dp"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/spinner_background"
android:hint="Quantity #"
android:textSize="20sp" />
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spUnits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="1dp"
android:layout_toRightOf="@id/etQty"
android:background="@drawable/spinner_background" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<EditText
android:id="@+id/tvPrice"
android:layout_width="170dp"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/spinner_background"
android:hint="Price per unit"
android:textSize="20sp" />
<EditText
android:id="@+id/tvSubtotal"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/text_background"
android:enabled="false"
android:hint="Subtotal"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spGstper"
android:layout_width="170dp"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="1dp"
android:layout_toLeftOf="@+id/etGstamt"
android:background="@drawable/spinner_background" />
<EditText
android:id="@+id/tvGstamt"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/text_background"
android:enabled="false"
android:hint="GST Amount"
android:textSize="20sp" />
</LinearLayout>
<EditText
android:id="@+id/tvTotalamt"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/text_background"
android:enabled="false"
android:hint="Total Amount"
android:textSize="20sp" />
<EditText
android:id="@+id/tvAmtpaid"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/spinner_background"
android:hint="Amount Paid"
android:textSize="20sp" />
<EditText
android:id="@+id/tvbalance"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="1dp"
android:background="@drawable/text_background"
android:enabled="false"
android:hint="Amount Receivable"
android:textSize="20sp" />
<Button
android:id="@+id/btcreate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#DDBB4D"
android:text="Create New Order"
android:textColor="#fff" />
<Button
android:id="@+id/btcancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#DDBB4D"
android:text="Cancel"
android:textColor="#fff" />
</LinearLayout>
</ScrollView>
</LinearLayout>

this is my Code:
I am getting the below Null pointer error. please help be debug this.


public class CreateNewOrder extends AppCompatActivity {
//declaring dropdowns
private Spinner spinner1, spinner2, spinner3;
//declaring Text fields
EditText txtSubtotal, txtGstamt, txtTotal, txtbalance, txtPrice, txtamtpaid, txtQty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new_order_pg);
//Hooks for txt fields
txtQty = findViewById(R.id.etQty);
txtPrice = findViewById(R.id.tvPrice);
txtSubtotal = findViewById(R.id.tvSubtotal);
txtGstamt = findViewById(R.id.tvGstamt);
txtTotal = findViewById(R.id.tvTotalamt);
txtamtpaid = findViewById(R.id.tvAmtpaid);
txtbalance = findViewById(R.id.tvbalance);
//Hook for subtotal Calculation
txtPrice.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) {
}
@Override
public void afterTextChanged(Editable editable) {
double pric = Double.parseDouble(txtPrice.getText().toString());
double quan = Double.parseDouble(txtQty.getText().toString());
double subtot = quan * pric;
txtSubtotal.setText((String.valueOf(subtot)));
}
});
</details>
# 答案1
**得分**: 2
在您的代码中似乎有一个拼写错误。EditText `R.id.etQty` 不存在。布局XML文件中具有id为`tvQty`的EditText。
<details>
<summary>英文:</summary>
There seems to be a typo in your code. The edittext `R.id.etQty` is nonexistent. The layout XML file has an EditText with the id `tvQty`.
</details>
# 答案2
**得分**: 0
将 EditText 的 id 从 "etQty" 更改为 "tvQty"。
<details>
<summary>英文:</summary>
change id of EditText from &quot;etQty&quot; to  &quot;tvQty&quot;
</details>

huangapple
  • 本文由 发表于 2020年9月3日 00:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63709499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定