在自定义对话框中获取空指针异常
getting null pointer exception inside custom dialogBox
嘿,我正在创建一个包含 2 个 editText 的自定义对话框。我不知道这段代码有什么问题,但每当用户点击提交时,它都会在 editText 中说空值。
任何人都可以帮忙吗?
下面是自定义对话框的代码
private void showPriceDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View dialogView = LayoutInflater.from(this).inflate(R.layout.content_price_filter,null);
//Button submitBtn = (Button)findViewById(R.id.submitBtn);
builder.setView(dialogView);
final android.support.design.widget.TextInputEditText minPrice = (android.support.design.widget.TextInputEditText)findViewById(R.id.minPrice);
final android.support.design.widget.TextInputEditText maxPrice = (android.support.design.widget.TextInputEditText)findViewById(R.id.maxPrice);
builder.setTitle("Set Price");
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String max = maxPrice.getText().toString();
String min = minPrice.getText().toString();
if (max.equals("") || min.equals("")){
Toast.makeText(getApplicationContext(),"empty fields",Toast.LENGTH_SHORT).show();
return;
}
mPriceValueTxt.setText(max + " Rs min :"+ min);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog al = builder.create();
al.show();
}
下面是自定义对话框Layout的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.support.design.widget.TextInputLayout
android:id="@+id/minPriceSelector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@+id/maxPriceSelector"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/minPrice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Min Price"
android:inputType="number"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/maxPriceSelector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintLeft_toRightOf="@+id/minPriceSelector"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/maxPrice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Max Price"
android:inputType="number"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>
这是我在点击提交时遇到的错误
09-03 13:11:30.180 22652-22652/com.example.ashis.propertysearch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ashis.propertysearch, PID: 22652
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.support.design.widget.TextInputEditText.getText()' on a null object reference
at com.example.ashis.propertysearch.FiltersActivity.onClick(FiltersActivity.java:290)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6204)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
你必须像下面那样做。
builder.setView(dialogView);
final android.support.design.widget.TextInputEditText minPrice = (android.support.design.widget.TextInputEditText) dialogView.findViewById(R.id.minPrice);
final android.support.design.widget.TextInputEditText maxPrice = (android.support.design.widget.TextInputEditText) dialogView.findViewById(R.id.maxPrice);
嘿,我正在创建一个包含 2 个 editText 的自定义对话框。我不知道这段代码有什么问题,但每当用户点击提交时,它都会在 editText 中说空值。
任何人都可以帮忙吗?
下面是自定义对话框的代码
private void showPriceDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View dialogView = LayoutInflater.from(this).inflate(R.layout.content_price_filter,null);
//Button submitBtn = (Button)findViewById(R.id.submitBtn);
builder.setView(dialogView);
final android.support.design.widget.TextInputEditText minPrice = (android.support.design.widget.TextInputEditText)findViewById(R.id.minPrice);
final android.support.design.widget.TextInputEditText maxPrice = (android.support.design.widget.TextInputEditText)findViewById(R.id.maxPrice);
builder.setTitle("Set Price");
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String max = maxPrice.getText().toString();
String min = minPrice.getText().toString();
if (max.equals("") || min.equals("")){
Toast.makeText(getApplicationContext(),"empty fields",Toast.LENGTH_SHORT).show();
return;
}
mPriceValueTxt.setText(max + " Rs min :"+ min);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog al = builder.create();
al.show();
}
下面是自定义对话框Layout的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.support.design.widget.TextInputLayout
android:id="@+id/minPriceSelector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@+id/maxPriceSelector"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/minPrice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Min Price"
android:inputType="number"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/maxPriceSelector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintLeft_toRightOf="@+id/minPriceSelector"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/maxPrice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Max Price"
android:inputType="number"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>
这是我在点击提交时遇到的错误
09-03 13:11:30.180 22652-22652/com.example.ashis.propertysearch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ashis.propertysearch, PID: 22652
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.support.design.widget.TextInputEditText.getText()' on a null object reference
at com.example.ashis.propertysearch.FiltersActivity.onClick(FiltersActivity.java:290)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6204)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
你必须像下面那样做。
builder.setView(dialogView);
final android.support.design.widget.TextInputEditText minPrice = (android.support.design.widget.TextInputEditText) dialogView.findViewById(R.id.minPrice);
final android.support.design.widget.TextInputEditText maxPrice = (android.support.design.widget.TextInputEditText) dialogView.findViewById(R.id.maxPrice);