Custom EditTextPreference: Unable to get TextView by ID

I created a custom EditTextPreference:

<EditTextPreference android:key="alert_planed_edittext_preference" android:title="@string/alert_planed_edittext_preference" android:summary="@string/alert_planed_summary_edittext_preference" android:dialogTitle="@string/alert_planed_dialog_title_edittext_preference" android:numeric="integer" android:layout="@layout/edit_text_preference_layout" /> 

custom layout code:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/alert_planed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginLeft="10dip" android:text="5" > </TextView> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="6dip" android:layout_marginLeft="10dip" android:layout_marginTop="6dip" > <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@android:id/title" android:layout_below="@android:id/title" android:maxLines="2" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout> </RelativeLayout> 

I just added a text view with id alert_planed.

In my preference activity, I'm trying to get a new EditTextPreference value and put it in my alert_planed text view. I get the value of EditTextPreference, this is normal. But when I set the text, I got an empty exception telling me that the TextView is null.

Here is my preference code:

 addPreferencesFromResource(R.xml.preferences); mTextViewAlertPlanned = (TextView) findViewById(R.id.alert_planed); findPreference("alert_planed_edittext_preference").setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange( android.preference.Preference arg0, Object value) { if(value != null) { Log.i("Preference","String.valueOf(value) ="+String.valueOf(value)); Log.i("Preference","mTextViewAlertPlanned ="+mTextViewAlertPlanned); int myVal = Integer.parseInt(String.valueOf(value)); mTextViewAlertPlanned.setText(myVal); } return true; } }); 

How can I get my TextView correctly (TextView is part of the custom layout of EditTextPreference)?

+1
source share
1 answer

Just inflate xml

 LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.custome_layout, null); mTextViewAlertPlanned = (TextView) view.findViewById(R.id.alert_planed); 
0
source

Source: https://habr.com/ru/post/1497981/


All Articles