0

我正在使用 Java 创建一个简单的 Click Counter Android 应用程序。我是 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 上收到一条错误消息,说“不幸的是,‘应用程序’已停止”。但是当我继续计数然后点击重置按钮时,它会再次将计数更改为零

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

崩溃日志:

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)
4

2 回答 2

2

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

于 2012-12-24T17:10:23.433 回答
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();


}



}
于 2012-12-24T20:46:21.237 回答