gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:05:14 24 4
gpt4 key购买 nike

我正在使用 Java 创建一个简单的点击计数器 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)

最佳答案

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

关于java - 按钮声音和重置按钮不起作用 Android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com