错误无法比拟的类型:Telegram 中的 Object 和 int

Error incomparable types: Object and int in Telegram

我导入了 Telegram 存储库。并尝试 运行 项目但在 Passcodeview.java 文件中出现上述错误。 它显示了此代码段中的错误

 @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    View rootView = getRootView();
    int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView);
    getWindowVisibleDisplayFrame(rect);
    keyboardHeight = usableViewHeight - (rect.bottom - rect.top);

    if (UserConfig.passcodeType == 1 && (AndroidUtilities.isTablet() || getContext().getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)) {
        int t = 0;
        if (passwordFrameLayout.getTag() != 0) {
            t = (Integer) passwordFrameLayout.getTag();
        }
        LayoutParams layoutParams = (LayoutParams) passwordFrameLayout.getLayoutParams();
        layoutParams.topMargin = t + layoutParams.height - keyboardHeight / 2 - (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0);
        passwordFrameLayout.setLayoutParams(layoutParams);
    }

    super.onLayout(changed, left, top, right, bottom);
}

虽然同一个项目运行曾经在我的另一台机器上正常运行过,但我没有做任何更改。

我认为你的另一台机器不满足条件

UserConfig.passcodeType == 1 && (AndroidUtilities.isTablet() || getContext().getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)

因此,下面的 "if" 永远不会在那里执行:

if (passwordFrameLayout.getTag() != 0) {
            t = (Integer) passwordFrameLayout.getTag();
        }

问题是你确实在比较对象(标签)和数字 0。

将代码更改为

if (passwordFrameLayout.getTag() != null) {
            t = (Integer) passwordFrameLayout.getTag();
        }

如果您确定标签在不为空的情况下始终为整数。

如果您不确定,请使用 try-catch (NumberFormatException)。

问题在线:if (passwordFrameLayout.getTag() != 0) {

getTag() returns 一个对象,您无法将其与整数常量进行比较。相反,您应该检查标签是否不为空。如果您不确定标签是否始终是 Integer,您也应该检查 instanceof

if (passwordFrameLayout.getTag() != null && passwordFrameLayout.getTag() instanceof Integer) {