我真的很困惑.我正在尝试做一个简单的文本交换器,它将增加数量并根据数量更新价格.现在在我的xml中,我在TextSwitcher中有类似TextView的东西,只是为了增加数量.我用findViewById(R.id.quantity)获取textview.
所以我必须找到设置增量的数量(我正在实现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));
谁能让我知道我做错了什么?我一直在这一行得到我的nullpointer:
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>
解决方法
从
Documentation for TextSwitcher:
setText (CharSequence text) Sets the text of the next view and
switches to the next view. This can be used to animate the old text
out and animate the next text in.
这意味着您将需要至少两个文本视图,一个包含旧文本,另一个用于接收新文本和动画.
以下XML应该有效:
<TextSwitcher android:id="@+id/counter" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TextSwitcher>
原文地址:https://www.jb51.cc/android/314243.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。