0

我真的很难过,我迫切希望找到解决方案:

我正在为我的应用程序中的片段使用 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));
}
}

一切正常。但是我想通过在方法中调用 FragmentDetails 类的更新方法来更新日期集上片段的文本视图onDateSet。但是使用视图或方法访问 textview 会getView()返回空指针异常

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

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

编辑

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

4

0 回答 0