问题描述
我收到以下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”