TextSwitcher
是ListView
行的一部分(在xml中定义)。
我在setViewValue中定义了TextSwitcher的文本:
switch(viewId) {
case R.id.tw_text:
TextSwitcher twText = (TextSwitcher) view;
twText.setCurrentText("test text");
return true;
如果twText
为TextView
,则此代码可以正常运行。但它返回带有TextSwitcher
的NullPointerException:
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): FATAL EXCEPTION: main
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): java.lang.NullPointerException
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.TextSwitcher.setCurrentText(TextSwitcher.java:89)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at my.package.MainActivity$1.setViewValue(MainActivity.java:87)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:126)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.ListView.onMeasure(ListView.java:1109)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.os.Looper.loop(Looper.java:123)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at java.lang.reflect.Method.invoke(Method.java:521)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): at dalvik.system.NativeStart.main(Native Method)
更新1 :looks like我需要将setFactory()添加到我的代码中。 但如果我做以下事情:
TextSwitcher twText = (TextSwitcher) view;
twText.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView t = new TextView(MainActivity.this);
return t;
}
});
我收到了另一个错误:
05-22 10:54:42.852: ERROR/AndroidRuntime(3907): java.lang.IllegalStateException: Can't add more than 2 views to a ViewSwitcher
05-22 10:54:42.852: ERROR/AndroidRuntime(3907): at android.widget.ViewSwitcher.addView(ViewSwitcher.java:64)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907): at android.widget.TextSwitcher.addView(TextSwitcher.java:67)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907): at android.view.ViewGroup.addView(ViewGroup.java:1845)
更新2 :我在twText.removeAllViews()
之前添加了twText.setFactory()
。它可以工作,但看起来不是删除和再次添加视图的最佳方法。如何检查视图是否已存在?
另外,我想在xml中保留TextSwitcher的格式/样式。 setFactory的使用不允许我这样做。我该怎么办?
答案 0 :(得分:6)
在调用setCurrentText
时,看起来没有向文本切换器添加任何视图。此方法基本上只获取当前文本视图并为其设置文本,因此如果出现NullPointerException
,则可能仅由于文本切换器中缺少视图而发生。
关于工厂的错误:您必须小心设置工厂或自己添加两个视图。您有以下任一选项:
setFactory
,这将创建两个视图。addView
手动添加两个视图。在这两种情况下,只能有两个视图,不多也不少。如果您设置了工厂并且已添加了一些视图,则会出现错误。您可以通过检查getChildCount()
方法返回的内容来检查是否需要设置工厂:
TextSwitcher switcher = (TextSwitcher) view;
if (switcher.getChildCount() != 2) {
switcher.removeAllViews();
switcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return new TextView(MainActivity.this);
}
});
}
答案 1 :(得分:0)
可能有点太迟了,但我在寻找答案时发现了你的问题。我在尝试调用setFactory时,在一个空的TextSwitcher中得到了类似的错误,无法添加2个以上的视图。当我将活动更改为扩展Activity而不是ActionBarActivity时,我的问题得到了解决。希望这将有助于将来的某些人。
TextSwitcher throwing "more than 2 views" error when setFactory() is called