如何为首选项中的搜索栏设置OnChangeListener(图片http://img825.imageshack.us/img825/6729/prefscreen.png)?我已经尝试过这段代码,但它将返回空指针异常:
PreferenceActivity:
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.SeekBar;
public class peferences extends PreferenceActivity {
private SeekBar SensitivitySeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SensitivitySeekBar = (SeekBar)findViewById(R.id.SeekBar2);
SensitivitySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
}
的preferences.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_accelerometer_title"
android:key="preference_accelerometer" >
<CheckBoxPreference
android:key="pref_key_x_axis"
android:summary="@string/preference_x_axis_summary"
android:title="Use x-axis"
android:defaultValue="true" android:order="1" android:enabled="true"/>
<CheckBoxPreference
android:title="Use y-axis"
android:key="pref_key_y_axis"
android:summary="@string/preference_y_axis_summary"
android:order="2"
android:enabled="true"
android:defaultValue="true"/>
<CheckBoxPreference
android:summary="@string/preference_z_axis_summary"
android:title="Use z-axis"
android:key="pref_key_z_axis"
android:order="3"
android:enabled="true"
android:defaultValue="true"/>
<CheckBoxPreference
android:title="Accelerometer value"
android:key="pref_accelerometer_value"
android:summary="@string/preference_show_accelerometer_value"/>
<Preference
android:layout="@layout/seekbar"
android:order="4"
android:key="seekbar"/>
</PreferenceCategory>
</PreferenceScreen>
搜索栏的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/Sensitivity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="@string/preference_sensitivity_title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/Sensitivity_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Sensitivity_title"
android:layout_below="@+id/Sensitivity_title"
android:text="@string/preference_sensitivity_summary" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Sensitivity_summary"
android:max="5000" />
</RelativeLayout>
日志:
12-26 22:55:35.204: D/dalvikvm(24291): GC_CONCURRENT freed 329K, 4% free 9212K/9572K, paused 4ms+5ms, total 29ms
12-26 22:55:36.626: D/AndroidRuntime(24291): Shutting down VM
12-26 22:55:36.626: W/dalvikvm(24291): threadid=1: thread exiting with uncaught exception (group=0x4154e930)
12-26 22:55:36.634: E/AndroidRuntime(24291): FATAL EXCEPTION: main
12-26 22:55:36.634: E/AndroidRuntime(24291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bluetoothc/com.example.bluetoothc.peferences}: java.lang.NullPointerException
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.os.Looper.loop(Looper.java:137)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 22:55:36.634: E/AndroidRuntime(24291): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 22:55:36.634: E/AndroidRuntime(24291): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 22:55:36.634: E/AndroidRuntime(24291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 22:55:36.634: E/AndroidRuntime(24291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 22:55:36.634: E/AndroidRuntime(24291): at dalvik.system.NativeStart.main(Native Method)
12-26 22:55:36.634: E/AndroidRuntime(24291): Caused by: java.lang.NullPointerException
12-26 22:55:36.634: E/AndroidRuntime(24291): at com.example.bluetoothc.peferences.onCreate(peferences.java:23)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.Activity.performCreate(Activity.java:5104)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 22:55:36.634: E/AndroidRuntime(24291): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 22:55:36.634: E/AndroidRuntime(24291): ... 11 more
12-26 22:55:39.001: I/Process(24291): Sending signal. PID: 24291 SIG: 9
答案 0 :(得分:1)
我遇到了同样的NPE问题,我通过定义我的SeekbarPreference
得到了它并在onBindDialogView
中注册了监听器。这是代码:
public class SeekbarPreference extends DialogPreference implements
OnSeekBarChangeListener {
private SeekBar mSeekbar;
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mSeekbar = (SeekBar) view.findViewById(R.id.sb_text_size);
mSeekbar.setOnSeekBarChangeListener(this);
}
public SeekbarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.setDialogLayoutResource(R.layout.setting_textsize_seekbar);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
// save to SharedPrefs, etc
}
}
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
这是我的preference.xml:
<PreferenceCategory
<com.flounder.xeniaNote.view.SeekbarPreference
android:dialogLayout="@layout/setting_textsize_seekbar"
android:key="@string/pref_text_size_key"
android:title="@string/pref_text_size" />
...
</PreferenceCategory>
我希望它有所帮助!