英文:
Why is EditText.getText() returning null?
问题
在我使用Android Studio制作我的第一个项目时,我尝试创建一个警报框,其中我可以从两个EditText中获取输入并将它们作为创建RecyclerView元素的参数,但结果是它返回null(即使EditText的ID已正确提及)。
fa = findViewById(R.id.add_alarm_fab);
fa.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this);
builder.setTitle("Add slots");
View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog_layout, null);
builder.setView(customLayout);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText editText1 = findViewById(R.id.edCentre);
EditText editText2 = findViewById(R.id.edSlots);
String centreName = editText1.getText().toString();
Integer slots = Integer.parseInt(editText2.getText().toString());
if (centreName != null && slots != null)
medCentreArrayList.add(new MedCentre(centreName, slots));
ad.notifyDataSetChanged();
}
});
builder.create().show();
}
});
这是用于警报对话框的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<EditText
android:id="@+id/edSlots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Slots available"
android:inputType="textPersonName"
android:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/edCentre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="20dp"
android:ems="10"
android:hint="Centre Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/edSlots"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
警报对话框布局的图像:
和警报对话框的图像:
英文:
So while I was making my first project in android studio, I tried making an alert box where I can take inputs from 2 EditTexts and take them as arguments for creating an RecyclerView Element, but it turns out it's returning null (even though IDs of the EditTexts are mentioned correctly)
fa = findViewById(R.id.add_alarm_fab);
fa.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this);
builder.setTitle("Add slots");
View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog_layout,null);
builder.setView(customLayout);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText editText1 = findViewById(R.id.edCentre);
EditText editText2 = findViewById(R.id.edSlots);
String centreName = editText1.getText().toString();
Integer slots = Integer.parseInt(editText2.getText().toString());
if (centreName!=null && slots!=null)
medCentreArrayList.add(new MedCentre(centreName,slots));
ad.notifyDataSetChanged();
}
});
builder.create().show();
}
});
This is my XML file for the alert dialog:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<EditText
android:id="@+id/edSlots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Slots available"
android:inputType="textPersonName"
android:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/edCentre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="20dp"
android:ems="10"
android:hint="Centre Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/edSlots"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案1
得分: 0
在对话框容器中查找您的视图,类似于以下内容:
EditText editText1 = customLayout.findViewById(R.id.edCentre);
英文:
Looks like you need to find your view in the dialog container, something like this:
EditText editText1 = customLayout.findViewById(R.id.edCentre);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论