嘿我正在创建一个包含2个editText的自定义对话框。我不知道这段代码有什么问题,但每当用户点击提交时,它都会在editText中说出空值。
有人可以为此提供帮助吗?
以下是自定义dialogBox的代码
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();
}
以下是自定义对话框布局
的布局<?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$1.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)
答案 0 :(得分:0)
你必须这样做。
for c, info in enumerate(searchKeys): # loop over searchKeys list
counter = 0 # set counter
for line in currentFile: # loop over lines in current file
if info in line: # search for info in line
counter += 1 # count +1 if info found
out.write( "\nInfo: " + str(info) + "\n" +
"Occurrence: " + str(counter) + "\n")