我真的坚持这一点。我正在尝试做一个简单的文本切换器,它将增加数量并根据数量更新价格。现在在我的 xml 中,我在 TextSwitcher 中有类似 TextView 的东西,只是为了增加数量。我得到了文本视图findViewById(R.id.quantity)
。
所以这是我必须找到的设置增量数量(我正在实现 ViewFactory)
switcher = (TextSwitcher) findViewById(R.id.switcher);
switcher.setFactory(this);
quantity = (TextView) findViewById(R.id.quantity);
我也覆盖了 makeView()
@Override
public View makeView() {
return quantity;
}
此外,当按下增量按钮时,我会增加计数器并将切换器上的文本设置为当前计数。像这样:
switcher.setText(String.valueOf(currentQuantity));
有人可以让我知道我做错了什么吗?我一直在这一行得到我的空指针:
switcher.setFactory(this);
这是 XML 片段:
<TextSwitcher android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/switcher">
<TextView android:text="TextView" android:id="@+id/quantity" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TextSwitcher>