按钮声音和重置按钮不起作用Android java

时间:2012-12-24 16:57:18

标签: java

我正在使用Java创建一个简单的Click Counter Android App。我是Java新手。以下是我的代码,gui有不同的功能按钮,例如计数按钮本身,重置计数按钮和静音按钮。

public class wazeefa extends Activity { 

//Count Button 
TextView txtCount; 
ImageView image; 
Button btnCount; 
Button wmute; 
Button wreset; 
public static int count=0; 
SharedPreferences app_preferences; 
MediaPlayer mpButtonClick; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// The activity is being created. 
setContentView(R.layout.wazeefa); 

//SAVE COUNT 
app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE); 


count = app_preferences.getInt("count", 0); 

txtCount = (TextView)findViewById(R.id.wcount); 
txtCount.setText("This app has been started " + count + " times."); 


txtCount = (TextView)findViewById(R.id.wcount); 
txtCount.setText("This app has been started " + count + " times."); 

//Button SOUND AND COUNT 
mpButtonClick = MediaPlayer.create(this, R.raw.bubble); 
//RESET Button 
wreset = (Button)findViewById(R.id.wreset); 

txtCount = (TextView)findViewById(R.id.wcount); 
txtCount.setText(String.valueOf(count)); 

btnCount = (Button)findViewById(R.id.wclick); 
btnCount.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
image = (ImageView) findViewById(R.id.imageview); 
count++; 
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage); 
if (count > 0) image.setImageResource(R.drawable.duroodimage); 
if (count > 9) image.setImageResource(R.drawable.zikrimage); 
if (count > 39) image.setImageResource(R.drawable.duroodimage); 
txtCount.setText(String.valueOf(count)); 
} 
}); 

wreset.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
count = 0; 
image.setImageResource(R.drawable.duroodimage);; 
txtCount.setText("0"); 
} 
}); 
} 

@Override 
protected void onPause() { 
super.onPause(); 
// save count value here 

SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", count); 
editor.commit(); 
}

//MUTE button
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.wmute:
AudioManager.setMode(AudioManager.MODE_IN_CALL);
AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
default:
AudioManager.setMode(AudioManager.MODE_NORMAL );
AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
break;
}

}

我有1个问题:

当我回到应用程序并且计数显示为'5'时,例如,重置按钮不再起作用 - 它什么都不做,我在AVD上收到一条错误消息,说'不幸'App'已停止'。但是,当我继续计数然后点击重置按钮时,它会再次将计数更改为零

关于上述问题的任何建议,我是否遗漏了任何内容或将代码置于错误的区域?在编辑代码之前,按钮声音正常工作,以保存“计数”数据。 如果我含糊不清,请告诉我......

崩溃日志:

12-24 18:07:42.661: W/Trace(3633): Unexpected value from nativeGetEnabledTags: 0
12-24 18:07:42.741: D/AndroidRuntime(3633): Shutting down VM
12-24 18:07:42.741: W/dalvikvm(3633): threadid=1: thread exiting with uncaught     exception (group=0x40a70930)
12-24 18:07:42.811: E/AndroidRuntime(3633): FATAL EXCEPTION: main
12-24 18:07:42.811: E/AndroidRuntime(3633): java.lang.NullPointerException
12-24 18:07:42.811: E/AndroidRuntime(3633):     at  com.shaadcorp.wazaifapp.wazeefa$2.onClick(wazeefa.java:81)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at android.view.View.performClick(View.java:4202)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at android.view.View$PerformClick.run(View.java:17340)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at android.os.Handler.handleCallback(Handler.java:725)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at  android.os.Handler.dispatchMessage(Handler.java:92)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at android.os.Looper.loop(Looper.java:137)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at java.lang.reflect.Method.invoke(Method.java:511)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 18:07:42.811: E/AndroidRuntime(3633):     at dalvik.system.NativeStart.main(Native     Method)
12-24 18:07:48.842: I/Process(3633): Sending signal. PID: 3633 SIG: 9
12-24 18:07:51.572: E/Trace(3808): error opening trace file: No such file or directory     (2)

2 个答案:

答案 0 :(得分:2)

我想我在代码中的任何地方都没有看到mpButtonClick.start()。这就是声音没有播放的原因。添加mpButtonClick.start()。到你的ClickListener。

答案 1 :(得分:1)

已修复问题,感谢您的帮助Passionate Androiden。

public class wazeefa extends Activity {

//Count Button
TextView txtCount;
ImageView image;
Button btnCount;
Button wmute;
Button wreset;
public static int count=0;
SharedPreferences app_preferences;
MediaPlayer mpButtonClick;
AudioManager audioManager;
public static boolean mutestatus=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.wazeefa);




audioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//SAVE COUNT
app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE);


count = app_preferences.getInt("count", 0);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");

image = (ImageView) findViewById(R.id.imageview);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");

//Button SOUND AND COUNT
mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
//RESET Button
wreset = (Button)findViewById(R.id.wreset);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));

btnCount = (Button)findViewById(R.id.wclick);

wmute=(Button)findViewById(R.id.wmute);

btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
}
});

wreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
});


wmute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!mutestatus){
mutestatus=true;
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
Log.v("'test....",""+mutestatus);
}
else{
mutestatus=false;
audioManager.setMode(AudioManager.MODE_NORMAL );
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
Log.v("'test....",""+mutestatus);
}
}});
}

@Override
protected void onPause() {
super.onPause();
// save count value here

SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();


}



}