Search code examples
androiddialogbutterknife

Null object reference for Button defined in Dialog using ButterKnife


I have a problem about defining objects in Dialog screen via ButterKnife. I can define a holder class to determie each object using in Dialog screen. When I run the app, it throws an error shown below.

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

How can I connect each object via ButterKnifer to the Dialog Screen?

How can I fix it?

Here is my code shown below.

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 file

<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>

Solution

  • My answer

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