I'm using a custom layout from a preference:
<PreferenceCategory
android:title="Intolleranze">
<Preference android:layout="@layout/intolerance_settings" />
</PreferenceCategory>
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextIntolerance"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Aggiungi intolleranza..." />
<Button
android:id="@+id/addIntoleranceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="@drawable/oval_button"
android:text="@string/add"
android:textColor="@android:color/white"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
When I try to add an OnClick Listener from the Preference Fragment I get a NullPointerException, because I cannot get a reference to the custom layout I set in the PreferenceScreen. How can I solve that ?
PreferenceFragment:
public class SettingsFragment extends PreferenceFragment {
private Set<String> set;
private Set<String> in;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
getActivity().setTitle("Impostazioni");
return view;
}
@Override
public void onViewCreated(View view, Bundle savedIstanceState){
Button addBtn = (Button) findViewById(R.id.addIntoleranceButton);
final EditText editText = (EditText) findViewById(R.id.editTextIntolerance);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//NullPointerException
}
});
}