gpt4 book ai didi

java - onKeyDown 堆栈溢出错误 Android

转载 作者:行者123 更新时间:2023-12-01 13:09:20 25 4
gpt4 key购买 nike

每当我长时间按下按钮(在本例中使用电源和较低音量进行屏幕截图)时,我的应用程序就会崩溃并出现 StackOverFlowError。

我很确定它必须处理我试图减少项目中重复代码数量的事实,而且我很确定我知道为什么。我不知道它是否可以在我的实用程序类中修复,或者我是否不应该这样做。我这样做的原因是因为我不想为一个非常简单的对话框持续编写大量代码。

实用类

public static boolean onKeyDown(final Activity activity, final Class<?> action, String message, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
ParseUser.logOut();
if (action == null) {
activity.onBackPressed();
return;
}
Intent intent = new Intent(activity, action);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);
break;

case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message).setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
return true;
}
return activity.onKeyDown(keyCode, event);
}

以及我如何调用它:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return Utilities.onKeyDown(this, null, "Do you want to exit the application", keyCode, event);
}

日志猫不断交替这些行:

at com.x.y.Login.onKeyDown(Login.java:187)
com.x.y.Utilities.onKeyDown(Utilities.java:45)

有什么想法吗?

最佳答案

一个简单的解决方案:

实用程序.java

/**
* @return true if keyEvent is consumed
*/
public static boolean onKeyDown(final Activity activity, final Class<?> action, String message, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

//...all you code here

return true;
}
return false;
}

登录.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(Utilities.onKeyDown(this, null, "Do you want to exit the application", keyCode, event)){
return true;
}
return super.onKeyDown(keyCode, event);
}

关于java - onKeyDown 堆栈溢出错误 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23015520/

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