I am new to Android and I need to create an AlertDialog when I press an specific button. AlertDialog has a Seekbar and the seekBar is used to change the volume for my application. But I cannot make it work. Can you please help me? Thank you.
I get this error :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setMax(int)' on a null object reference
and my code looks like this:
public class Setting extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public AlertDialog AlarmVolume(View view) {
/*
This part of code creates a dialog which has a seekbar for volume.
*/
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.volume_dialog, (ViewGroup) findViewById(R.id.settingsItem));
builder.setView(v).setTitle("Adjust Alarm Volume").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO save new volume amount
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
SeekBar seekbarVolume = (SeekBar)findViewById(R.id.volumeSeekBar);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
seekbarVolume.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
seekbarVolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));
seekbarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, 0);
}
});
return builder.create();
}
}
and this is my volum_dialog.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/volumeSeekBar"
android:layout_marginTop="15sp"
android:layout_weight="1" />
</LinearLayout>
You are trying to find your seekbar in your contextView (R.layout.activity_setting):
SeekBar seekbarVolume = (SeekBar)findViewById(R.id.volumeSeekBar);
It returns null because you created your SeekBar dynamically for your AlertDialog:
View v = inflater.inflate(R.layout.volume_dialog, (ViewGroup) findViewById(R.id.settingsItem));
To solve this issue, change this line (SeekBar)findViewById(R.id.volumeSeekBar);
to:
(SeekBar) v.findViewById(R.id.volumeSeekBar);
It tries to find your Seekbar in View v
instead of ContextView which should work fine.