Cannot find TextView in PreferenceActivity
In my PreferenceActivity I have some Preferences. One I inflate from layout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.settings);
...
}
protected void onResume() {
super.onResume();
Pre开发者_Python百科ference registrationStatus = (Preference) findPreference("registrationStatusPref");
if (!TextUtils.isEmpty(somestring){
registrationStatus.setLayoutResource(R.layout.push_reg_status);
TextView push_reg_status_txt = (TextView)findViewById(R.id.pushRegTxtPref);
}
....
}
push_reg_status_txt
is a TextView from push_reg_status
layout, and push_reg_status_txt
is always null.
push_reg_status layout:
....
<TextView
android:id="@+id/pushRegTxtPref"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
android:text="Registration Succesful"
android:textColor="#99ff33"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"/>
.....
Why?
Preference.setLayoutResource()
only updates the internal reference that preference has to a layout ID, it does not actually update the layout to redisplay. Therefore the TextView you are looking for with findViewById()
is not inflated for your use. The only place Preferences inflate their layouts is when created.
You'll either need to have that custom layout set at the start (before addPreferencesFromResource()
inflates everything), or adjust the title/summary properties of the existing preference instead to set your "Registration Successful" string. Also, if you are using a custom preference layout, make sure you're following the rules set forth in the SDK Documentation.
Hope that Helps!
Maybe because you calls findViewById of your Activity instead of calling same method of registrationStatus object.
精彩评论