【发布时间】:2021-06-10 18:35:07
【问题描述】:
我有一个包含自定义布局的首选项屏幕:
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="myKey"
android:layout="@layout/my_layout" />
<ListPreference
app:defaultValue="@string/defaultGender"
app:entries="@array/gender_entries"
app:entryValues="@array/gender_values"
app:key="gender"
app:title="@string/gender"
app:useSimpleSummaryProvider="true" />
我正在尝试在 my_layout 中设置 TextView 的值:
public static class MyFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference_screen, rootKey);
TextView textView= (TextView) this.getActivity().findViewById(R.id.myTextView);
textView.setText("XXX");
}
}
但我在textView.setText("XXX"); 上收到NullPointerException
我做错了什么?
【问题讨论】:
标签: android android-fragments nullpointerexception android-preferences