Question

I'm really having hard time and im desperate to find a solution for this:

I'm using ABS library for fragments in my application.

This is the onCreateView method in my fragment class:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    view = inflater.inflate(R.layout.details_fragment, container, false);

    prefs = getSherlockActivity().getSharedPreferences(prefName, getSherlockActivity().MODE_PRIVATE);

    day = Integer.parseInt(prefs.getString(BIRTH_KEY, "").substring(0, 2));
    month = Integer.parseInt(prefs.getString(BIRTH_KEY, "").substring(3, 5));
    year = Integer.parseInt(prefs.getString(BIRTH_KEY, "").substring(6, 10));

    name = (TextView)view.findViewById(R.id.nameEdit);
    age  = (TextView)view.findViewById(R.id.ageEdit);


    name.setText(prefs.getString(FIRST_NAME_KEY, "") + " " + prefs.getString(LAST_NAME_KEY, ""));
    age.setText(getAge(year, month, day) + " years");

    age.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDatePickerDialog();

        }
    });
    return view;
}

public void update() {
    System.out.println(getView());
}

This DateDialogPicker class:

public class DatePickerFragment extends SherlockDialogFragment implements OnDateSetListener {

private SharedPreferences prefs;
private String prefName = "UserPref";
private static final String BIRTH_KEY = "date_of_birth";

private Date dateObj;
private FragmentDetails frag;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    prefs = getSherlockActivity().getSharedPreferences(prefName, getSherlockActivity().MODE_PRIVATE);

    frag = new FragmentDetails();

    final Calendar c = Calendar.getInstance();
    int day = Integer.parseInt(prefs.getString(BIRTH_KEY, "").substring(0, 2));
    int month = (Integer.parseInt(prefs.getString(BIRTH_KEY, "").substring(3, 5))-1);
    int year = Integer.parseInt(prefs.getString(BIRTH_KEY, "").substring(6, 10));

    return new DatePickerDialog(getSherlockActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    // TODO Auto-generated method stub

    String _date = (dayOfMonth + "/" + (monthOfYear+1) + "/" + year);
    SharedPreferences.Editor editor = prefs.edit();

    editor.putString(BIRTH_KEY, formatDate(_date));
    editor.commit();

    FragmentDetails frag = new FragmentDetails();
    frag.update();

}   

private String formatDate(String date) {
    SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
    try {
        dateObj = curFormater.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return (curFormater.format(dateObj));
}
}

Everything works fine. But I want to update textview of fragment on Date set, by calling update method of FragmentDetails class in onDateSet method. But accessing textview using view or method getView() returns null pointer exeption.

In this case method update() prints getView() and its result is null.

How can i get the View of the fragment so I can access and manage textviews of this view.

EDIT

So far i found out that i can access any view of fragment while im in the fragment instance but when i create a new dialog i cant getView of that fragment from dialog instance.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top