NPE в android .text.Editable android .widget.EditText.getText () 'для ссылки на нулевой объект - PullRequest
1 голос
/ 06 мая 2020

У меня есть этот NPE, когда я вызываю getText () в EditText. У меня есть предупреждение, и я установил свой макет как представление для предупреждения, и мне нужно получить текст из EditText. Спасибо за любую помощь

Вот мой код

        LinearLayout view;
        EditText input;
        Button in;
        Button gfxtr;
        Button gfh;

        private void numItem(int position) {
            view = (LinearLayout) getLayoutInflater()
                    .inflate(R.layout.dialog, null);
            input = (EditText) findViewById(R.id.input);
            in = (Button) findViewById(R.id.in);
            gfxtr = (Button) findViewById(R.id.gfxtr);
            gfh = (Button) findViewById(R.id.gfh);

            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Введите новое количество");

            alert.setView(view);



            alert.setPositiveButton("ДА", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String qq = input.getText().toString();


                }
            });


            alert.setNegativeButton("НЕТ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });
            alert.show();
        }

XML:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical">


            <Button
                android:id="@+id/in"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:onClick="in"
                android:scaleType="fitCenter"
                android:text="шт"></Button>

            <Button
                android:id="@+id/gfxtr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="gfxtr"
                android:scaleType="fitCenter"
                android:text="пачек"></Button>

            <Button
                android:id="@+id/gfh"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="gfh"
                android:scaleType="fitCenter"
                android:text="пар"></Button>
        </LinearLayout>
    </LinearLayout>

I получите эту ошибку:

java .lang.NullPointerException: попытка вызвать виртуальный метод 'android .text.Editable android .widget.EditText.getText ()' для нулевого объекта ссылка

1 Ответ

1 голос
/ 06 мая 2020

Вы не вызываете findViewById при просмотре. Попробуйте так:

input = (EditText) view.findViewById(R.id.input);
...