微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android的NullPointerException错误addTextChangedListener

如何解决Android的NullPointerException错误addTextChangedListener

我收到以下Null指针错误。请帮助进行调试。 我收到以下Null指针错误。请帮忙调试一下。

   
    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)
       

这是我的布局:当我尝试运行代码时,它给了我一个nullpointerexception。任何帮助将不胜感激。

我收到以下Null指针错误。请帮忙调试一下。

<?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>

这是我的代码: 我收到以下Null指针错误。请帮忙调试一下。





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 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)));
           }
       });

解决方法

您的代码中似乎有错字。编辑文本R.id.etQty不存在。布局XML文件具有一个ID为tvQty的EditText。

,

将EditText的ID从“ etQty”更改为“ tvQty”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?