Java 在首选项片段中使用SeekBar的自定义首选项
我正在尝试设置一个Java 在首选项片段中使用SeekBar的自定义首选项,java,android,android-preferences,seekbar,preferencefragment,Java,Android,Android Preferences,Seekbar,Preferencefragment,我正在尝试设置一个SeekBar,从首选项中控制音乐流的音量。一切看起来都很正常,但当我访问设置屏幕时,我收到一个异常,说明我正试图在NullObjectReference上调用seekbar.setMax(int)。我不明白为什么它没有得到对对象的引用 SeekBar xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_p
SeekBar
,从首选项中控制音乐流的音量。一切看起来都很正常,但当我访问设置屏幕时,我收到一个异常,说明我正试图在NullObjectReference
上调用seekbar.setMax(int)
。我不明白为什么它没有得到对对象的引用
SeekBar xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/seek_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="Alarm Volume"
android:textColor="@android:color/black"
android:textSize="16sp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"/>
<TextView
android:id="@+id/seek_bar_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:text="11"
android:textColor="@android:color/black"
android:textSize="16sp"
android:layout_marginEnd="24dp"
android:layout_marginTop="16dp"/>
<SeekBar
android:id="@+id/alarm_volume_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_below="@id/seek_bar_title"
android:max="15"/>
您应该在设置片段onCreate()
中调用initVolumeControl()
。如果这样做,则必须将其设置为静态,这会导致内存泄漏。我认为可能需要重写onCreateView,并在那里获取对SeekBar的引用。我尝试了几种不同的方法来设置它,但无法使其正常工作。我在设置片段的onCreate中添加了这一行:((AppPreferenceActivity)getActivity()).initVolumeControl();但是我仍然会遇到同样的错误。有人想帮我吗?……您应该在设置片段onCreate()中调用initVolumeControl()
)
。如果我这样做,那么我必须将其设置为静态,这会导致内存泄漏。我认为可能需要重写onCreateView并在那里获取对SeekBar的引用。我尝试了几种不同的方法来设置它,但无法使其正常工作。我在设置片段的onCreate中添加了这一行:((AppPreferenceActivity)getActivity()).initVolumeControl();不过我还是犯了同样的错误。有人想帮我吗?。。。。。。。。。
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="SETTINGS">
<Preference
android:layout="@layout/seekbar_preference" />
<CheckBoxPreference
android:title="Countdown Notification"
android:summary="show timers in notification tray"
android:key="countdown_notification"
android:defaultValue="true" />
<CheckBoxPreference
android:title="Keep Screen On"
android:summary="keep screen on and app in foreground"
android:key="keep_screen_on"
android:defaultValue="false" />
</PreferenceCategory>
public class AppPreferenceActivity extends PreferenceActivity {
TextView seekBarValue;
AudioManager audioManager = null;
SeekBar alarmVolumeSeekBar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
initVolumeControl();
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
public void initVolumeControl() {
try {
alarmVolumeSeekBar = (SeekBar) findViewById(R.id.alarm_volume_seek_bar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekBarValue = (TextView) findViewById(R.id.seek_bar_value);
int alarmVolumeMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int progress = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
alarmVolumeSeekBar.setMax(alarmVolumeMax);
alarmVolumeSeekBar.setProgress(progress);
alarmVolumeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
alarmVolumeSeekBar.setProgress(progress);
seekBarValue.setText(progress);
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}