EditText.getText() 返回 null 的原因是什么?

huangapple go评论63阅读模式
英文:

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>

警报对话框布局的图像:

EditText.getText() 返回 null 的原因是什么?

和警报对话框的图像:

EditText.getText() 返回 null 的原因是什么?

英文:

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(&quot;Add slots&quot;);

                View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog_layout,null);
                builder.setView(customLayout);

                builder.setPositiveButton(&quot;Add&quot;, 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 &amp;&amp; slots!=null)
                            medCentreArrayList.add(new MedCentre(centreName,slots));
                        ad.notifyDataSetChanged();
                    }
                });
                builder.create().show();
            }
        });

This is my XML file for the alert dialog:-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;

    &lt;EditText
        android:id=&quot;@+id/edSlots&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;20dp&quot;
        android:ems=&quot;10&quot;
        android:hint=&quot;Slots available&quot;
        android:inputType=&quot;textPersonName&quot;
        android:padding=&quot;20dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

    &lt;EditText
        android:id=&quot;@+id/edCentre&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;20dp&quot;
        android:padding=&quot;20dp&quot;
        android:ems=&quot;10&quot;
        android:hint=&quot;Centre Name&quot;
        android:inputType=&quot;textPersonName&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/edSlots&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Image of alert dialog layout:
EditText.getText() 返回 null 的原因是什么?

And Image of alert dialog:
EditText.getText() 返回 null 的原因是什么?

答案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);

huangapple
  • 本文由 发表于 2023年2月16日 18:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定