简体   繁体   中英

How to get the value of our custom AlertDialog?

I created a custom AlertDialog and I want to save the values that the user has set. However I can't access them because Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference .

I am in HomeFragment, I created the AlertDialog and set the layout. I did it like this :

  public class HomeFragment extends Fragment implements CustomDialog.CustomDialogListener {

    private HomeViewModel homeViewModel;
    private View root;

    public View onCreateView(@NonNull final LayoutInflater inflater,
                             final ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        Button btn_add_apero = (Button) root.findViewById(R.id.btn_add_apero);
        btn_add_apero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(root.getContext());
                builder.setTitle("Super un nouvel apéro !");
                final EditText name_apero = (EditText)root.findViewById(R.id.edit_apero);
                final EditText date_apero = (EditText)root.findViewById(R.id.edit_date);

                builder.setView(inflater.inflate(R.layout.layout_dialog, null))
                        // Add action buttons
                        .setPositiveButton(R.string.text_add, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                LaperoDatabase db = Room.databaseBuilder(root.getContext(),
                                        LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();

                                AperoDao dbApero = db.getAperoDao();
                                Apero new_apero = new Apero(name_apero.getText().toString(), date_apero.getText().toString());
                                dbApero.insert(new_apero);
                            }
                        })
                        .setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                builder.show();
            }
        });
        return root;
    }

In my layout_dialog.xml I set the 2 EditText :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <EditText
        android:id="@+id/edit_apero"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/edit_apero" />

    <EditText
        android:id="@+id/edit_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_apero"
        android:layout_alignParentStart="true"
        android:hint="@string/edit_date"
        android:layout_alignParentLeft="true" />

</RelativeLayout>  

How can I manage to get the value that are in those 2 fields ?

First thing i noticed is that you're using root to find your dialog views, but root points to your fragment layout. root = inflater.inflate(R.layout.fragment_home, container, false);

First inflate your dialog layout before referencing its resource id.

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View inflater = getLayoutInflater().inflate(R.layout.layout_dialog, null);
builder.setView(inflater);

Then change root to inflater so you reference the view from your dialog layout

 final EditText name_apero = (EditText)inflater.findViewById(R.id.edit_apero);
 final EditText date_apero = (EditText)inflater.findViewById(R.id.edit_date);

Full code

Button btn_add_apero = (Button) root.findViewById(R.id.btn_add_apero);
btn_add_apero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            View inflater = getLayoutInflater().inflate(R.layout.layout_dialog, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setView(inflater);
            builder.setTitle("Super un nouvel apéro !");

            final EditText name_apero = (EditText)inflater.findViewById(R.id.edit_apero);
            final EditText date_apero = (EditText)inflater.findViewById(R.id.edit_date);
                        // Add action buttons
                     builder.setPositiveButton(R.string.text_add, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                LaperoDatabase db = Room.databaseBuilder(root.getContext(),
                                        LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();

                                AperoDao dbApero = db.getAperoDao();
                                Apero new_apero = new Apero(name_apero.getText().toString(), date_apero.getText().toString());
                                dbApero.insert(new_apero);
                            }
                        })
                     builder.setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                builder.show();
            }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM