使用 ButterKnife 在 Dialog 中定义的 Button 的空对象引用

Null object reference for Button defined in Dialog using ButterKnife

我在通过 ButterKnife 在对话框屏幕中定义对象时遇到问题。 我可以定义一个 holder class 来确定在对话框屏幕中使用的每个对象。 当我 运行 应用程序时,它会抛出如下所示的错误。

java.lang.NullPointerException: Attempt to read from field 'android.widget.Button com.example.dialog.AlertDialogActivity$ViewHolder.buttonUyeOl' on a null object reference

如何通过 ButterKnifer 将每个对象连接到对话框屏幕?

我该如何解决?

下面是我的代码。

public class AlertDialogActivity extends AppCompatActivity {

    @BindView(R.id.dialogAc)
    Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        ButterKnife.bind(this);

        openDialog();
    }

    private void openDialog() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDialogScreen();

            }
        });
    }

    private void openDialogScreen() {
        LayoutInflater ınflater = this.getLayoutInflater();
        View view = ınflater.inflate(R.layout.alert_dialog, null);
        final ViewHolder holder;

        if (view != null) {
            holder = (ViewHolder) view.getTag();

        } else {
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setView(view);
        alert.setCancelable(false);
        final AlertDialog dialog = alert.create();



        holder.buttonUyeOl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), holder.mailEditText.getText().toString() + " "
                                + holder.kadiEditText.getText().toString() + " " + holder.sifreEditText.getText().toString()
                        , Toast.LENGTH_LONG).show();
                dialog.cancel();
            }
        });

        holder.buttonCik.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        dialog.show();

    }


    static final class ViewHolder {

        @BindView((R.id.mailAdres))
        EditText mailEditText;

        @BindView(R.id.kullanici)
        EditText kadiEditText;

        @BindView(R.id.sifre)
        EditText sifreEditText;

        @BindView(R.id.buttonUyeOl)
        Button buttonUyeOl;

        @BindView(R.id.buttonExit)
        Button buttonCik;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}

XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#324589"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/kullanici"
        android:hint="Kullanıcı İsmini Giriniz"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mailAdres"
        android:hint="Mail Adresi Giriniz"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sifre"
        android:inputType="textPassword"
        android:hint="Şifre Giriniz"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Uye Ol"
            android:id="@+id/buttonUyeOl"
            />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="İptal Et"
            android:id="@+id/buttonExit"
            />

    </LinearLayout>


</LinearLayout>

我的回答

private void openDialogScreen() {
   LayoutInflater ınflater = this.getLayoutInflater();
   View view = ınflater.inflate(R.layout.alert_dialog, null);

   final ViewHolder holder = new ViewHolder(view);

....
}