Toast crashes application
In my application I've implemented such that pressing the hardware volume keys displays a Toast with the current volume. Things go well most of the times. However long presses of the volume keys do succeed in crashing my application. The Log generated on crash is the following :
....
11-19 06:43:22.114 I/NotificationService( 2783): enqueueToast pkg=com.FunTime.android callback=android.app.ITransientNotification$Stub$Proxy@47b679a8 duration=0
11-19 06:43:22.119 V/WindowManager( 2783): Delivering key 25 to Window{47f6a930 com.FunTime.android/com.FunTime.android.map.MapActivity paused=false}
11-19 06:43:22.164 I/NotificationService( 2783): enqueueToast pkg=com.FunTime.android callback=android.app.ITransientNotification$Stub$Proxy@47a980b0 duration=0
11-19 06:43:22.169 V/WindowManager( 2783): Delivering key 25 to Window{47f6a930 com.FunTime.android/com.FunTime.android.map.MapActivity paused=false}
11-19 06:43:22.209 I/NotificationService( 2783): enqueueToast pkg=com.FunTime.android callback=android.app.ITransientNotification$Stub$Proxy@47b4d270 duration=0
...many such notifications...
11-19 06:43:22.244 E/SurfaceFlinger( 2783): createSurface() failed, generateId = -12
11-19 06:43:22.244 W/WindowManager( 2783): OutOfResourcesException creating surface
11-19 06:43:22.244 I/WindowManager( 2783): Out of memory for surface! Looking for leaks...
11-19 06:43:22.244 W/WindowManager( 2783): No leaked surfaces; killing applicatons!
11-19 06:43:22.244 W/ActivityManager( 2783): Killing processes for memory at adjustment 0
11-19 06:43:22.244 W/ActivityManager( 2783): Killing for memory: ProcessRecord{47dd75c8 3246:com.FunTime.android/10079} (adj 0)
11-19 0开发者_如何学Python6:43:22.244 I/Process ( 2783): Sending signal. PID: 3246 SIG: 9
11-19 06:43:22.244 W/WindowManager( 2783): Looks like we have reclaimed some memory, clearing surface for retry.
11-19 06:43:22.244 W/WindowManager( 2783): Due to memory failure, waiting a bit for next layout
The approach I follow: I have a toast Object and for every key press event detected I inflate a view ,set the view elements within, set it to the toast and then show the toast.
Any idea what is going wrong ?
Lated Edited
In the base activity class from which all my activities inherit:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN)
{
myAudioManager.getInstance().onVolumeDown();
myAudioManager.getInstance().showVolumeAdjustment(myAudioManager.getInstance().getVolume());
return true;
}
else if(keyCode==KeyEvent.KEYCODE_VOLUME_UP)
{
myAudioManager.getInstance().onVolumeUp();
myAudioManager.getInstance().showVolumeAdjustment(myAudioManager.getInstance().getVolume());
return true;
}
return super.onKeyDown(keyCode, event);
}
In myAudioManager
public void showVolumeAdjustment(final int adjustment)
{
volumeToast.setDuration(Toast.LENGTH_SHORT);
volumeToast.setView(getVolumeProgressBarView(adjustment));
volumeToast.setGravity(Gravity.TOP,0,0);
volumeToast.show();
}
private View getVolumeProgressBarView(int adjustment)
{
LayoutInflater inflater = (LayoutInflater) myApplication.getInstance().getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
toastView= inflater.inflate(R.layout.volume_toast, null);
ProgressBar pb =(ProgressBar)toastView.findViewById(R.id.level);
pb.setMax(MAX_VOICE_VOLUME_ENGINE);
pb.setProgress(adjustment);
return toastView;
}
You make too many toasts, and fill up the memory.
Try putting a timer, or something to limit the number of toasts generated.
精彩评论