在片段中调用getView将返回null

时间:2012-07-22 17:13:52

标签: android view dialog null fragment

我真的很难,我很想找到解决方案:

我在我的应用程序中使用ABS库作为片段。

这是我的片段类中的onCreateView方法:

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());
}

这个DateDialogPicker类:​​

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));
}
}

一切正常。但是我想通过在onDateSet方法中调用FragmentDetails类的update方法来更新Date set上的fragment的textview。但是使用视图或方法getView()访问textview会返回空指针执行

在这种情况下,方法update()打印getView(),其结果为空。

如何获取片段的视图,以便我可以访问和管理此视图的文本视图。

修改

到目前为止,我发现我可以在片段实例中访问片段的任何视图,但是当我创建一个新的对话框时,我不能从对话框实例中找到该片段的getView

0 个答案:

没有答案