I am trying to get an instance of a SeekBar using the findViewById()
and attaching a listener to it.
The code looks like this:
SeekBar seek_bar = findViewById(R.id.audio_volume);
seek_bar.setOnSeekBarChangeListener(listener);
And the listener:
private SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
log.d("YO HERE");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
Followed this SO post, and invoked the findViewById
inside the onCreate
method after these two lines:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
But the problem persists. I am still getting:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
And the app fails to open. How can I solve this issue? Also it would be very helpful if the process of debugging a null object reference error is shared.
Layout xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListPreference
android:key="PREF_MIN_REC"
android:title="@string/min_rec"
android:summary="@string/min_rec_desc"
android:entries="@array/min_rec_options"
android:entryValues="@array/min_rec_values"
android:dialogTitle="@string/min_rec"
android:defaultValue="15"
/>
<ListPreference
android:key="PREF_AUDIO_SOURCE"
android:title="@string/audio_source"
android:summary="@string/audio_source_desc"
android:entries="@array/audio_source_options"
android:entryValues="@array/audio_source_values"
android:dialogTitle="@string/audio_source"
android:defaultValue="4"
/>
<ListPreference
android:key="PREF_AUDIO_FORMAT"
android:title="@string/audio_format"
android:summary="@string/audio_format_desc"
android:entries="@array/audio_format_options"
android:entryValues="@array/audio_format_values"
android:dialogTitle="@string/audio_format"
android:defaultValue="1"
/>
<org.anasthase.androidseekbarpreference.SeekBarPreference
android:id="@+id/volume"
android:key="PREF_MIC_VOLUME"
android:title="@string/mic_volume"
android:summary="@string/mic_volume_desc"
app:maxValue="100"
app:minValue="1"
app:stepValue="1"
android:defaultValue="20"
app:format="%.1f"
app:displayDividerValue="5"/>
<org.anasthase.androidseekbarpreference.SeekBarPreference
android:id="@+id/audio_volume"
android:key="PREF_AUDIO_VOLUME"
android:title="@string/audio_volume"
android:summary="@string/audio_volume_desc"
app:maxValue="100"
app:minValue="1"
app:stepValue="1"
android:defaultValue="20"
app:format="%.1f"
app:displayDividerValue="5"/>
<EditTextPreference
android:key="PREF_URL_SUBDOMAIN"
android:title="@string/server_subdomain"
android:summary="@string/server_subdomain_desc"
android:dialogTitle="@string/server_subdomain"
android:defaultValue=".telforce.biz"
/>
<ListPreference
android:key="PREF_UPLOAD_NETWORK"
android:title="@string/upload_network"
android:summary="@string/upload_network_desc"
android:entries="@array/upload_network_options"
android:entryValues="@array/upload_network_values"
android:dialogTitle="@string/upload_network"
android:defaultValue="1"
/>
<CheckBoxPreference
android:key="save_term_enable_key"
android:title="@string/save_term_check_title"
android:summary="@string/save_term_check_summary"/>
<EditTextPreference
android:key="save_term_key"
android:title="@string/save_term_title"
android:summary="@string/save_term_summary"
android:dependency="save_term_enable_key"
android:defaultValue="@string/save_term_default_value"
android:dialogTitle="@string/save_term_dialog"/>
</PreferenceScreen>