我真的被困在这个问题上了。我正在尝试做一个简单的文本切换器,它将增加一个数量,并更新一个基于数量的价格。现在,在我的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>发布于 2011-11-25 06:28:47
从Documentation for TextSwitcher
setText (CharSequence text)设置下一个视图的文本并切换到下一个视图。这可以用来动画旧的文本输出和动画的下一个文本。
这意味着您至少需要两个文本视图,一个包含旧文本,另一个用于接收新文本并在其中生成动画。下面的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>发布于 2011-06-15 04:17:52
确保在查找TextSwitcher之前调用了setContentView
发布于 2011-09-27 23:41:29
Mmm..interesting,我在这里也遇到了同样的问题在我的例子中有两个问题,首先我从makeView返回null。我认为这不是你的情况,因为你是从findViewById获取它的引用(注意,有时这个方法会失败并返回一个空引用,我建议你在那里放置一个断点,并确保你没有一个空指针)。
我遇到的第二个问题(我想这可能也是你的问题)是,显然TextSwitcher不希望有任何子视图,所以你不应该在它里面放一个TextView。尝试删除该TextView,看看它是否工作。
https://stackoverflow.com/questions/6349052
复制相似问题