【问题标题】:NullPointerException: Attempt to invoke virtual methodNullPointerException:尝试调用虚方法
【发布时间】:2017-01-06 04:14:20
【问题描述】:

尝试调用虚方法: void android.widget.Button.setTag(java.lang.Object)
在空对象引用上。

当我在按钮上使用androiddataBinding 时出错:

<Button
    android:id="@+id/btn_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="50dp"
    android:background="@{userInfo.isEmpty?@drawable/shape_gray_corner:@color/colorWhite}"
    android:text="@string/text_login"
    android:onClick="onViewClik"
    android:textColor="@color/colorWhite"
    android:textSize="@dimen/textsize_24" />

【问题讨论】:

  • 您是否使用了绑定标签并将按钮正确绑定到您的活动?
  • 是的,android:background="@drawable/shape_gray_corner" 没问题,只是在使用时出错 android:background="@{userInfo.isEmpty?@drawable/shape_gray_corner:@color/colorWhite}"
  • 你能在你的模型和 userInfo.getImage 中以编程方式在drawable中检查这个吗?还有你在检查一个对象是否为空?检查 userInfo.name 类似的东西
  • 我会尝试使用表达式更改按钮背景
  • 一个是可绘制的,另一个是彩色的。这是我认为的问题。 android:background="@{userInfo.isEmpty ? @color/colorWhite : @color/your_default_color}" 请试试这个

标签: android android-databinding java.lang


【解决方案1】:

我认为您必须尝试使用​​默认颜色而不是可绘制的 或者在两个地方都使用drawable。

喜欢这个

android:background="@{userInfo.isEmpty ? @color/colorYellow : @color/colorWhite}"

【讨论】:

  • 谢谢,我试过了,但不行我用另一种方法解决了 android:background="@drawable/selector_button_enable" android:enabled="@{!TextUtils.isEmpty(userInfo.login_name)&&amp ;!TextUtils.isEmpty(userInfo.password)}"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 2016-01-25
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多