-1

I have a button in listview row that when I click on it I want a dialogFragment to be open and set the text of an edit text (that located inside the dialogFragment) to some String.

The problem is: the app shut down when it comes to the line of the settext method.

This is the code I use to open the dialogFragment and set text to it.

public void onClick(View v) {
    FragmentManager manager = getFragmentManager();
    View parentRow = (View) v.getParent();
    ListView listView = (ListView) parentRow.getParent();
    final int position = listView.getPositionForView(parentRow);
    TrempData data = adapter.getItem(position); //from here im getting the data that i want to set to the edit text. 
    Addtremp trempDialog = new Addtremp();
    trempDialog.show(manager, "Addtremp");
    trempDialog.from.setText(data.get_from());
    trempDialog.to.setText(data.get_to());
    trempDialog.date.setText(data.get_date());
    trempDialog.time.setText(data.get_time());
    trempDialog.extra.setText(data.get_extras());
}

Hope someone could help me.

Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nauruto
  • 143
  • 1
  • 12
  • so you got an error can you show it to us please , it seems tremdata is a setter/getter class and contain empty values – Moudiz Aug 20 '17 at 10:46
  • Put the full stack trace. You possibly have uninitialized fields and are calling `setText` on null. – orip Aug 20 '17 at 10:48
  • This is the error i get Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference but i get all the data i need. I checked it and print it to log to see it – Nauruto Aug 20 '17 at 10:53

1 Answers1

0

Your App will surely crash due to NullPointerException. Because you are trying to set data on UI which is not rendered yet.

What steps should follow?

  1. Pass data to DialogFragment which is going to display on UI in a form of arguments.

  2. Create callback which will inform you when UI is rendered on Dialog. Check this Callback to a Fragment from a DialogFragment . On getting listener you could set data on your UI components.

Personally I prefer solution1 and for that you should read passing argument to DialogFragment

Rahul
  • 10,457
  • 4
  • 35
  • 55
  • Thank you. I understand the problem now but I'm not sure i to use your solution. I'm kind of new to android development... – Nauruto Aug 20 '17 at 11:10
  • Pass your data from one screen to another and set data in your fragment when your view created. – Rahul Aug 20 '17 at 11:11
  • My data is located in listview row. the listview its inside an activity. I get the data with no problem but i dont understand how to pass it to the the dialogfragment – Nauruto Aug 20 '17 at 11:23
  • read this https://stackoverflow.com/questions/15459209/passing-argument-to-dialogfragment – Rahul Aug 20 '17 at 11:24