在我的应用程序中,我实现了按下硬件音量键,显示当前音量的Toast。事情大部分时间都很顺利。但是,长按音量键确实会导致我的应用程序崩溃。崩溃时生成的日志如下:
....
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 06: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
我遵循的方法: 我有一个toast对象,并且对于检测到的每个按键事件,我都会给视图充气,设置视图元素,将其设置为toast然后显示toast。
知道出了什么问题吗?
Lated Edited
在我的所有活动继承的基本活动类中:
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);
}
在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;
}
答案 0 :(得分:2)
你制作了太多的祝酒词,并填满了记忆。
尝试使用计时器或其他东西来限制生成的吐司数量。