Well you didn't show anything, so i'm just guessing here.
Here an example of custom preferences.
import android.content.*;
import android.preference.*;
import android.widget.*;
public final class Preferences
{
private static Preferences instance;
private final SharedPreferences prefs;
private final SharedPreferences.Editor editor;
public final TextView textView;
private final Preferences(final Context context) {
instance = this;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
editor = prefs.edit();
}
public static final Preferences getInstance(final Activity activity) {
if(instance == null) {
instance = new Preferences(activity);
textView = activity.findViewById(R.id.my_textView);
}
return instance;
}
/*****************
* UTILITY *
*****************/
public final void putBoolean(final String name, final boolean value) {
editor.putBoolean(name, value);
editor.commit();
}
public final boolean getBoolean(final String name, final boolean defaultValue) {
return prefs.getBoolean(name, defaultValue);
}
public final void putInt(final String name, final int value) {
editor.putInt(name, value);
editor.commit();
}
public final int getInt(final String name, final int defaultValue) {
return prefs.getInt(name, defaultValue);
}
public final void putFloat(final String name, final float value) {
editor.putFloat(name, value);
editor.commit();
}
public final float getFloat(final String name, final float defaultValue) {
return prefs.getFloat(name, defaultValue);
}
public final void putLong(final String name, final long value) {
editor.putLong(name, value);
editor.commit();
}
public final long getLong(final String name, final long defaultValue) {
return prefs.getLong(name, defaultValue);
}
}
Then you can just do this. Make sure to call this inside of a class that extends Activity
.
Preferences prefs = Preferences.getInstance(this); //this is an Activity's instance.
prefs.textView.setText("Hello World");
EDIT
import android.content.*;
import android.preference.*;
import android.util.*;
import android.view.*;
import android.widget.*;
public class MyCustomPreference extends Preference
{
public TextView textView;
public ImageButton imageButton;
public MyCustomPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public MyCustomPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyCustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomPreference(Context context) {
super(context);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder)
{
super.onBindViewHolder(holder);
if(textView == null) {
textView = view.findViewById(R.id.my_textView);
imageButton = view.findViewById(R.id.my_imageButton);
}
}
public TextView getTextView(Activity activity) {
if(textView == null) return activity.findViewById(R.id.my_textView);
return textView;
}
public ImageButton getImageButton(Activity activity) {
if(imageButton == null) return activity.findViewById(R.id.my_imageButton);
return imageButton;
}
}
Hopefully this will resolve your NullPointerException problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…