• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How can I access my custom AlertDialog EditText field ?

J

Jerome_heia

Guest
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 :
Java:
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 !");
              
               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) {
                               EditText name_apero = (EditText)root.findViewById(R.id.edit_apero);
                               EditText date_apero = (EditText)root.findViewById(R.id.edit_date);
                               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 :
Code:
<?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 ? I tried with a breakpoint and the R.id.edit_apero in this line
Code:
EditText name_apero = (EditText) root.findViewById(R.id.edit_apero);
has a number but then it assign it as null
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones