Java 应用程序在实现静音按钮时崩溃
我编写了一个测试,现在我在FOR查询下设置了一个mediaplayer,每当用户点击正确答案时,声音就会播放出来。现在,在同一个活动中,我希望bttOFF将静音活动的声音,我如何才能做到这一点?我用mp.setVolume(0,0)设置了一个onClickListener;但该应用程序在重启时崩溃。谢谢你的关注DJava 应用程序在实现静音按钮时崩溃,java,android,audio,nullpointerexception,android-mediaplayer,Java,Android,Audio,Nullpointerexception,Android Mediaplayer,我编写了一个测试,现在我在FOR查询下设置了一个mediaplayer,每当用户点击正确答案时,声音就会播放出来。现在,在同一个活动中,我希望bttOFF将静音活动的声音,我如何才能做到这一点?我用mp.setVolume(0,0)设置了一个onClickListener;但该应用程序在重启时崩溃。谢谢你的关注D public class QuizActivity extends AppCompatActivity { private ActionBarDrawerToggl
public class QuizActivity extends AppCompatActivity {
private ActionBarDrawerToggle mToggle;
private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
Dialog dialog;
Dialog dialog2;
TextView closeButton;
TextView closeButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
final MediaPlayer mp = new MediaPlayer();
//Dialog 1
createDialog();
Button dialogButton = (Button) findViewById(R.id.dialogbtn);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//end Dialog 1
//Dialog 2
createDialog2();
Button dialogButton2 = (Button) findViewById(R.id.dialogbtn2);
dialogButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.show();
}
});
closeButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
//end Dialog 2
Button bttON = (Button)findViewById(R.id.bttON);
bttON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.setVolume(0,0);
}
});
TextView shareTextView = (TextView) findViewById(R.id.share);
shareTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello!");
myIntent.putExtra(Intent.EXTRA_TEXT, "My highscore in Quizzi is very high! I bet you can't beat me except you are cleverer than me. Download the app now! https://play.google.com/store/apps/details?id=amapps.impossiblequiz");
startActivity(Intent.createChooser(myIntent, "Share with:"));
}
});
mQuestionLibrary.shuffle();
setSupportActionBar((Toolbar) findViewById(R.id.nav_action));
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Able to see the Navigation Burger "Button"
((NavigationView) findViewById(R.id.nv1)).setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_stats:
startActivity(new Intent(QuizActivity.this, Menu2.class));
break;
case R.id.nav_about:
startActivity(new Intent(QuizActivity.this, Menu3.class));
break;
}
return true;
}
});
mScoreView = (TextView) findViewById(R.id.score_score);
mQuestionView = (TextView) findViewById(R.id.question);
mButtonChoice1 = (Button) findViewById(R.id.choice1);
mButtonChoice2 = (Button) findViewById(R.id.choice2);
mButtonChoice3 = (Button) findViewById(R.id.choice3);
final List<Button> choices = new ArrayList<>();
choices.add(mButtonChoice1);
choices.add(mButtonChoice2);
choices.add(mButtonChoice3);
updateQuestion();
for (final Button choice : choices) {
choice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (choice.getText().equals(mAnswer)) {
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("sample.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
updateScore();
updateQuestion();
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score", mScore); // pass score to Menu2
startActivity(intent);
}
}
});
}
}
private void updateQuestion() {
if (mQuestionNumber < mQuestionLibrary.getLength()) {
mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));
mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber++);
} else {
Toast.makeText(QuizActivity.this, "Last Question! You are very intelligent!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score", mScore);
startActivity(intent);
}
}
private void updateScore() {
mScoreView.setText(String.valueOf(++mScore));
SharedPreferences mypref = getPreferences(MODE_PRIVATE);
int highScore = mypref.getInt("highScore", 0);
if (mScore > highScore) {
SharedPreferences.Editor editor = mypref.edit();
editor.putInt("highScore", mScore);
editor.apply();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
private void createDialog() {
dialog = new Dialog(this);
dialog.setTitle("Tutorial");
dialog.setContentView(R.layout.popup_menu1_1);
closeButton = (TextView) dialog.findViewById(R.id.closeTXT);
}
private void createDialog2() {
dialog2 = new Dialog(this);
dialog2.setTitle("Settings");
dialog2.setContentView(R.layout.popup_menu1_2);
closeButton2 = (TextView) dialog2.findViewById(R.id.closeTXT2);
}
如MediaPlayer类[此处](,float))的文档所示,所需的参数是float。您可以尝试这样做:
public void onClick(View v) {
mp.setVolume( 0.0, 0.0 );
}
看看这能不能解决你的问题
设置音量
在API级别1中添加
void setVolume(float leftVolume,float right volume)
设置此播放机的音量。建议使用此API来平衡应用程序中音频流的输出。除非您编写应用程序来控制用户设置,否则此API应优先于设置特定类型的所有流的音量的setStreamVolume(int,int,int)
。请注意,传递的体积值是0.0到1.0范围内的原始标量。UI控件应按对数缩放
参数
leftVolume float: left volume scalar
rightVolume float: right volume scalar
如MediaPlayer类[此处](,float))的文档所示,所需的参数是float。您可以尝试这样做:
public void onClick(View v) {
mp.setVolume( 0.0, 0.0 );
}
看看这能不能解决你的问题
设置音量
在API级别1中添加
void setVolume(float leftVolume,float right volume)
设置此播放机的音量。建议使用此API来平衡应用程序中音频流的输出。除非您编写应用程序来控制用户设置,否则此API应优先于设置特定类型的所有流的音量的setStreamVolume(int,int,int)
。请注意,传递的体积值是0.0到1.0范围内的原始标量。UI控件应按对数缩放
参数
leftVolume float: left volume scalar
rightVolume float: right volume scalar
从您的
closeButton
和closeButton2
进入您的findViewById
和closeButton2
添加您的日志,先生!哪一行是96?太棒了,我找不到错误/修复空指针链接96=bttON.setOnClickListener(新视图.OnClickListener(){谢谢你,先生!请添加你的logcat添加了先生!哪一行是96?太棒了,我找不到错误/修复空指针链接96=bttON.setOnClickListener(新视图.OnClickListener()){谢谢,它成功了,先生。NullPointerException
notIllegalArgumentexception
@oussemaroua在我创建答案时,注释不可见,但你是对的。不起作用。。没有问题,你对设置的卷有权现在对我起作用。谢谢!它是NullPointerException
notIllegalArgumentexception
@OUSSEMAROUA在我创建答案时,该注释不可见,但你是对的。不起作用。.没有问题,你对设置卷的看法正确,现在对我有效,谢谢!我在最后几行代码中已经这样做了,先生,但错误来自mpsetvolume。.你的问题是在null视图上添加一个clickListener
,而不是mp、 重新阅读抛出的例外情况我是新的,您能帮我吗,先生?bttON为空,R.id.bttON
不在您正在使用的同一布局中。此按钮位于另一个布局弹出菜单中……这是一个在此活动中出现的对话框啊,好的,我了解如何从我的主要活动中的布局实现此按钮?我没有在最后几行代码中,先生,错误来自mpsetvolume。您的问题是在null视图上添加一个clickListener
,与mp无关,请重新阅读抛出的例外我是新的,先生,您能帮我吗?bttON为null,R.id.bttON
与您正在使用的布局不同,因为此按钮位于另一个布局弹出窗口中向上菜单1\u 2…这是一个出现在本活动中的对话框啊,好的,我了解如何从主活动的布局中实现此按钮?