-2

I´ve stumbled across the following problem coding my app:

Whenever the statement Txtname.setText(name) is not suppressed, the app keeps crashing. Even when I hardcode string in, there it crashes. Via the toast I was able to see that the string name is passed correctly.

    //OnListpress
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
            Log.d(TAG, "onItemClick: name:" + thelist.get(i));
            String name = thelist.get(i);
            Showpopup(name);
        }

        //PopUP
        private void Showpopup(final String name) {
            myDialog = new Dialog(getActivity());
            myDialog.setContentView(R.layout.popup_list);
            myDialog.show();
            EditText Txtname = getActivity().findViewById(R.id.TxtNAME);

            Txtname.setText(name);
            Toast.makeText(getActivity(),name,Toast.LENGTH_LONG).show();
        }
    });

I found a few topics regarding this problem, but they always used Fragments as basis for the edittext. Mine is a dialog and the presented ways don't seem to work for me. I hope somebody knows the fix, thanks in advance!

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
  • Your `findViewById()` can't find an `EditText` with the id `R.id.TxtNAME` because you are doing something wrong. Where's that `EditText` supposed to be? If in the dialog, then you need to find it some other way. – Markus Kauppinen Mar 26 '20 at 18:03
  • That´s exactly what I´m trying to do... Do you know why that doesn´t work? And what can/should I do instead? – Spudermann Webz Mar 26 '20 at 18:48

1 Answers1

0

Make your:

EditText Txtname variable global....i.e. declare it globally and then call it like:

Txtname = ....

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20