带有 seekBar 的弹出 AlertDialog 以更改应用程序音量
Popup AlertDialog with a seekBar to change app volume
我是 Android 的新手,我需要在按下特定按钮时创建一个 AlertDialog。 AlertDialog 有一个 Seekbar,seekBar 用于更改我的应用程序的音量。但我无法让它发挥作用。你能帮我么?谢谢。
我收到这个错误:
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'void android.widget.SeekBar.setMax(int)'
我的代码如下所示:
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();
}
}
这是我的 volum_dialog.xml 文件:
<?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>
您正试图在您的 contextView (R.layout.activity_setting) 中找到您的搜索栏:
SeekBar seekbarVolume = (SeekBar)findViewById(R.id.volumeSeekBar);
它 returns 为空,因为您为 AlertDialog 动态创建了 SeekBar:
View v = inflater.inflate(R.layout.volume_dialog, (ViewGroup) findViewById(R.id.settingsItem));
要解决此问题,请将此行 (SeekBar)findViewById(R.id.volumeSeekBar);
更改为:
(SeekBar) v.findViewById(R.id.volumeSeekBar);
它试图在 View v
而不是 ContextView 中找到您的 Seekbar,后者应该可以正常工作。
我是 Android 的新手,我需要在按下特定按钮时创建一个 AlertDialog。 AlertDialog 有一个 Seekbar,seekBar 用于更改我的应用程序的音量。但我无法让它发挥作用。你能帮我么?谢谢。
我收到这个错误:
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'void android.widget.SeekBar.setMax(int)'我的代码如下所示:
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();
}
}
这是我的 volum_dialog.xml 文件:
<?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>
您正试图在您的 contextView (R.layout.activity_setting) 中找到您的搜索栏:
SeekBar seekbarVolume = (SeekBar)findViewById(R.id.volumeSeekBar);
它 returns 为空,因为您为 AlertDialog 动态创建了 SeekBar:
View v = inflater.inflate(R.layout.volume_dialog, (ViewGroup) findViewById(R.id.settingsItem));
要解决此问题,请将此行 (SeekBar)findViewById(R.id.volumeSeekBar);
更改为:
(SeekBar) v.findViewById(R.id.volumeSeekBar);
它试图在 View v
而不是 ContextView 中找到您的 Seekbar,后者应该可以正常工作。