2015-12-31 1 views
1

J'ai essayé de faire une mise en page de préférence personnalisée, parce que je voulais faire un sélecteur de couleur avec elle.Comment atteindre les éléments de la mise en page en utilisant PreferenceFragment?

J'utilise la solution here:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefFragment()).commit(); 
} 

Je voulais modifier la valeur de la TextView sur la mise en page, c'est la raison pour laquelle j'ai ajouté les lignes avec le TextView en elle. Le problème est qu'il a lancé une exception NullPointerException sur la ligne setText, parce que je pense que la méthode findViewById() a retourné null.

PrefFragment.java:

public class PrefFragment extends PreferenceFragment { 

    private TextView titleTV; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     titleTV = (TextView) getActivity().findViewById(R.id.title); 
     titleTV.setText(R.string.pref_bgcolor); 
    } 
} 

preferences.xml:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
<Preference 
    android:title="@string/pref_bgcolor" 
    android:summary="@string/pref_bgcolor_sum" 
    android:key="hu.atsoft.myip.color" 
    android:widgetLayout="@layout/color_pref"/> 
</PreferenceScreen> 

color_pref.xml:

<?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" 
    android:id="@+id/color_preference"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:paddingLeft="5dip" 
     android:paddingRight="5dip" 
     android:paddingTop="5dip" 
     android:singleLine="true" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@android:color/black"/> 

    <TextView 
     android:id="@+id/summary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/title" 
     android:singleLine="true" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 

    <ImageView 
     android:id="@+id/color_preview" 
     android:layout_width="20dip" 
     android:layout_height="20dip" 
     android:background="@android:color/darker_gray" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_margin="5dip"/> 

</RelativeLayout> 

Que dois-je utiliser pour atteindre les éléments au lieu du findViewById () méthode?

+0

Où puisez-vous PrefFragment? –

+0

Déplacez findviewbyId dans onActivityCreated dans PrefFragment. –

+0

Il lève toujours la même exception NullPointerException, sur la ligne setText(). –

Répondre

1

Vous devez utiliser personnalisée préférence:

PreferenceWithTip.class

public class PreferenceWithTip extends Preference { 
    private static final String TAG = "PreferenceWithTip"; 
    String pTitle = null; 
    String tipstring = null; 

    @SuppressLint("Recycle") 
    public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     Log.i(TAG,"PreferenceWithTip invoked"); 
     TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip); 
     tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring); 
     pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring); 
     ta.recycle(); 
    } 

    public PreferenceWithTip(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    @Override 
    protected void onBindView(View view) { 
     super.onBindView(view); 
     TextView pTitleView = (TextView)view.findViewById(R.id.title); 
     pTitleView.setText(pTitle); 
     TextView pTipView = (TextView)view.findViewById(R.id.summary); 
     pTipView.setText(tipstring); 
    } 

    @Override 
    protected View onCreateView(ViewGroup parent) { 
     return LayoutInflater.from(getContext()).inflate(R.layout.color_pref, 
       parent, false); 
    } 


} 

attr.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="PreferenceWithTip"> 
     <attr name="tipstring" format="string"></attr> 
     <attr name="titlestring" format="string"></attr> 
    </declare-styleable> 
</resources> 

preferences.xml:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
<com.test.mytest.PreferenceWithTip 
      preference:tipstring="a" 
      preference:titlestring="title" > 
     </com.test.mytest.PreferenceWithTip> 
</PreferenceScreen>