1

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>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Abrar
  • 6,874
  • 9
  • 28
  • 41
  • post layout resource also. – adnbsr Feb 28 '19 at 01:03
  • Added layout @adnbsr – Abrar Feb 28 '19 at 01:04
  • Tried adding this but got `Inconvertible types; cannot cast 'android.view.View' to 'org.anasthase.androidseekbarpreference.SeekBarPreference'` – Abrar Feb 28 '19 at 01:09
  • can you post your whole layout? – Faisal Feb 28 '19 at 01:11
  • you should check library's docs. It should be used in prefs layout. – adnbsr Feb 28 '19 at 01:12
  • do this way if its preference fragment: `SeekBarPreference skp = (SeekBarPreference) findPreference(R.id.audio_volume);` – Faisal Feb 28 '19 at 01:16
  • @Faisal updated with full layout. Tried adding `SeekBarPreference seek_bar = (SeekBarPreference) findPreference(R.id.audio_volume);` but isn't findPreference deprecated as of API level 16? – Abrar Feb 28 '19 at 01:21
  • 1
    do this way if its preference fragment: `SeekBarPreference skp = (SeekBarPreference) findPreference("PREF_AUDIO_VOLUME");` – Faisal Feb 28 '19 at 01:22
  • 1
    I don't think its deprecated. have a look at my gradle: `android { compileSdkVersion 28 defaultConfig { applicationId "" minSdkVersion 21 targetSdkVersion 28 versionCode 18 versionName "1.2.18" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }` minimum sdk is 21 and I'm using it. – Faisal Feb 28 '19 at 01:24
  • 1
    check my second comment. Instead of passing id, pass key and it'll find your preference. – Faisal Feb 28 '19 at 01:27
  • @Faisal Thanks man! It's working alright now! – Abrar Feb 28 '19 at 01:29
  • @Faisal Can you please add it as answer as well? Will be helpful for all – Abrar Feb 28 '19 at 01:30

2 Answers2

1

In your XML your audio_volume view belongs to class org.anasthase.androidseekbarpreference.SeekBarPreference

In your Java code, it is SeekBar only, so if it is not crashing, I assume Seekbar extends SeekBarPreference.

Ignore the casting comments and responses since it's not needed since last year if you're using the latest versions of java, android sdk and target api.

The null pointer exception means the seekbar was not found on the layout. Check if you have more than 1 xml with the same name (for different screen resolutions).

Isaac Urbina
  • 1,295
  • 11
  • 21
0

do this way if its preference fragment:

SeekBarPreference skp = (SeekBarPreference) findPreference("PREF_AUDIO_VOLUME"); 

Cheers ;)

Faisal
  • 705
  • 3
  • 16