2012-12-24 164 views
0

我正在使用Java創建一個簡單的Click Counter Android應用程序。我是Java新手。下面是我的代碼,gui有不同的功能按鈕,例如計數按鈕本身,重置計數按鈕和靜音按鈕。按鈕聲音和重置按鈕不起作用Android java

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」只是舉例,Reset按鈕不再起作用 - 它什麼都不做,我得到一個錯誤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) 
+0

你能粘貼崩潰日誌嗎? –

+0

是的,確定添加日誌 – Mustafa

回答

1

固定問題,感謝您的幫助激情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(); 


} 



} 
2

我想我沒看到mpButtonClick.start()在你的代碼的任何地方。這就是爲什麼聲音不能播放。添加mpButtonClick.start()。到你的ClickListener。

+1

在設置文本之前,您不必每次調用findViewById()。正如你已經聲明它是一個全局變量,你只需要調用它一次。 –