Android:无法在片段中设置 TextView 值
Android: Can't set TextView value in fragment
所以我今天开始使用片段,我试图在片段打开时设置 TextView 值。值可以更改,所以我将它们全部打包到 1 个函数中,以便可以调用它来更新数据。
这里:
public void loadDetails(){
ShortcutFunc sf = new ShortcutFunc(this.getActivity());
ConversePrefs cp = new ConversePrefs(this.getActivity());
sf.setTextValue(getActivity(), R.id.profile_name, cp.GetStringSetting("name", "unset") + " " + cp.GetStringSetting("surname", "unset"));
String dob = cp.GetStringSetting("dob", "unset");
String bod[] = dob.split("\-");
LocalDate birthdate = new LocalDate (Integer.valueOf(bod[2]), Integer.valueOf(bod[1]), Integer.valueOf(bod[0]));
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);
sf.setTextValue(getActivity(), R.id.profile_bd, dob + " (" + String.valueOf(age) + ")");
sf.setTextValue(this.getActivity(), R.id.profile_country, cp.GetStringSetting("country", "unset").toString());
sf.setTextValue(this.getActivity(), R.id.profile_email, cp.GetStringSetting("email", "unset").toString());
String joined[] = cp.GetStringSetting("join", "unset").split("\s+");
String joinedArray[] = joined[0].split("\-");
LocalDate joindate = new LocalDate(Integer.valueOf(joinedArray[0]), Integer.valueOf(joinedArray[1]), Integer.valueOf(joinedArray[2]));
LocalDate today = new LocalDate();
//Years joinedThen = Years.yearsBetween(joindate, today);
Days joinedThen = Days.daysBetween(joindate, today);
sf.setTextValue(this.getActivity(), R.id.profile_join, joined[0] + " (" + joinedThen.toString() + " Days)");
sf.setTextValue(this.getActivity(), R.id.profile_user, cp.GetStringSetting("username", "unset").toString());
}
ShortcutFunc 包含此函数:
public void setTextValue(Activity act, int id, String text){
TextView tv = (TextView)act.findViewById(id);
tv.setText(text);
}
我用它来节省时间和设置 TextViews 而无需做所有声明等。
我在 tv.setText(text) ShortcutFunc 函数上得到 NullPointerException。
这就是我调用 loadDetails() 的方式:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadDetails();
}
所以怎么了?
这些 TextView 是您的 Fragment
的成员,而不是持有 Fragment
的 Activity
的成员。您应该从您在 OnCreateView()
方法
中为您的片段膨胀的视图初始化它们
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_view, container, false);
textView = (TextView) view.findViewById(R.id.my_text_view);
return view;
}
所以我今天开始使用片段,我试图在片段打开时设置 TextView 值。值可以更改,所以我将它们全部打包到 1 个函数中,以便可以调用它来更新数据。 这里:
public void loadDetails(){
ShortcutFunc sf = new ShortcutFunc(this.getActivity());
ConversePrefs cp = new ConversePrefs(this.getActivity());
sf.setTextValue(getActivity(), R.id.profile_name, cp.GetStringSetting("name", "unset") + " " + cp.GetStringSetting("surname", "unset"));
String dob = cp.GetStringSetting("dob", "unset");
String bod[] = dob.split("\-");
LocalDate birthdate = new LocalDate (Integer.valueOf(bod[2]), Integer.valueOf(bod[1]), Integer.valueOf(bod[0]));
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);
sf.setTextValue(getActivity(), R.id.profile_bd, dob + " (" + String.valueOf(age) + ")");
sf.setTextValue(this.getActivity(), R.id.profile_country, cp.GetStringSetting("country", "unset").toString());
sf.setTextValue(this.getActivity(), R.id.profile_email, cp.GetStringSetting("email", "unset").toString());
String joined[] = cp.GetStringSetting("join", "unset").split("\s+");
String joinedArray[] = joined[0].split("\-");
LocalDate joindate = new LocalDate(Integer.valueOf(joinedArray[0]), Integer.valueOf(joinedArray[1]), Integer.valueOf(joinedArray[2]));
LocalDate today = new LocalDate();
//Years joinedThen = Years.yearsBetween(joindate, today);
Days joinedThen = Days.daysBetween(joindate, today);
sf.setTextValue(this.getActivity(), R.id.profile_join, joined[0] + " (" + joinedThen.toString() + " Days)");
sf.setTextValue(this.getActivity(), R.id.profile_user, cp.GetStringSetting("username", "unset").toString());
}
ShortcutFunc 包含此函数:
public void setTextValue(Activity act, int id, String text){
TextView tv = (TextView)act.findViewById(id);
tv.setText(text);
}
我用它来节省时间和设置 TextViews 而无需做所有声明等。 我在 tv.setText(text) ShortcutFunc 函数上得到 NullPointerException。
这就是我调用 loadDetails() 的方式:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadDetails();
}
所以怎么了?
这些 TextView 是您的 Fragment
的成员,而不是持有 Fragment
的 Activity
的成员。您应该从您在 OnCreateView()
方法
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_view, container, false);
textView = (TextView) view.findViewById(R.id.my_text_view);
return view;
}