我正在尝试设置SeekBar
来控制Preferences
内的音乐流量。一切似乎都运行正常但是当我访问设置屏幕时,我得到一个例外,说明我试图在seekbar.setMax(int)
上拨打NullObjectReference
。我不明白为什么它没有得到对象的引用。
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"/>
偏好屏幕:
<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();
}
}
}